2012-04-24 2 views
1

私は、設定アクティビティを持つウィジェットを持っています。ここで、ユーザはカラーピッカーからウィジェットの背景色を選択できます。私はImageViewを持っている下のメソッドを使用して、ImageViewで動的に設定するビットマップを作成しています。ウィジェットで動的に設定されたImageViewの角を丸くしましたか?

http://konsentia.com/2011/03/dynamically-changing-the-background-color-in-android-widgets/

public static Bitmap getBackground (int bgcolor) 
{ 
try 
    { 
     Bitmap.Config config = Bitmap.Config.ARGB_8888; // Bitmap.Config.ARGB_8888 Bitmap.Config.ARGB_4444 to be used as these two config constant supports transparency 
     Bitmap bitmap = Bitmap.createBitmap(2, 2, config); // Create a Bitmap 

     Canvas canvas = new Canvas(bitmap); // Load the Bitmap to the Canvas 
     canvas.drawColor(bgcolor); //Set the color 

     return bitmap; 
    } 
    catch (Exception e) 
    { 
     return null; 
    } 
} 

その後、私も何をしたいのか

remoteViews.setImageViewBitmap(R.id.bgcolor, getBackground(bgcolor)); 

は、彼らがウィジェット上の角を丸くしたい場合、ユーザーも選択できます。色とウィジェットが丸みを帯びているかどうかを動的に変更することは可能ですか?私が四隅を丸めて見た例から、ビットマップを設定する前にエッジを丸めることができるように、ビューの寸法を知る必要があるようです。私はこれはウィジェット内からは可能だとは思わないが...どんなアイデア?

答えて

4

これを行うための2つの方法があります。

  1. は大まかしたいウィジェットのサイズである(既存のメソッドを使用して)、正方形/角丸のビットマップを作成します。ユーザーがウィジェットのサイズを変更したり、画面解像度/ DPIを使用してデバイス上で使用すると、ビットマップが歪んでしまう恐れがあります。
  2. ビットマップリソースに白い四角形の角が丸くてRemoteViews.setIntウィジェットの背景の色/透明度を変更するImageView(Froyo以降が必要)。

    if(roundCorners) 
        remoteViews.setImageViewResource(R.id.widget_background, R.drawable.round_corners); 
    else 
        remoteViews.setImageViewResource(R.id.widget_background, R.drawable.square_corners); 
    
    remoteViews.setInt(R.id.widget_background, "setColorFilter", someColor); 
    remoteViews.setInt(R.id.widget_background, "setAlpha", someAlphaLevel); 
    

私は両方の方法を使用して、最大の互換性のために(2)をお勧めしてきました。

+0

ありがとうございます!これはまさに私が探していたものです... – timothyjc

+0

こんにちは!私はちょうどこの質問に非常に関連しているhttp://stackoverflow.com/questions/25877515/appwidget-image-with-rounded-cornersの質問をここで尋ねました。どんな洞察も素晴らしいだろう! –

関連する問題