我有一个带有配置活动的小部件,用户可以在其中从颜色选择器中选择小部件背景的颜色.我正在使用下面的方法,其中我有一个 ImageView 并创建了一个我在 ImageView 上动态设置的位图.
I have a widget with a config activity, where the user can select a color for the background of the widget from a color picker. I am using the method below where I have an ImageView and create a Bitmap which I dynamically set on the ImageView.
http://konsentia.com/2011/03/dynamically-changed-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));
我想要做的是让用户也可以选择他们是否想要小部件上的圆角.是否可以动态更改颜色以及小部件是否具有圆角?从我看过的圆角示例中,您似乎需要知道视图的尺寸,以便在设置位图之前可以圆角边缘.我认为这在小部件中是不可能的……有什么想法吗?
What I want to do though is let the user also choose if they want rounded corners on the widget. Is it possible to dynamically change both the color and whether the widget has rounded corners? From the examples I have looked at for rounding corners it seems you need to know the dimensions of the View so that you can round the edges before setting the Bitmap. I don't think this is possible from within a widget though... any ideas?
有两种方法:
创建一些带圆角的白色9 补丁位图资源角和方角并使用 RemoteViews.setInt 更改小部件背景 ImageView 的颜色/透明度(需要 Froyo 或更高版本),例如
Create some white 9 patch bitmap resources with rounded corners and square corners and use RemoteViews.setInt to change the color/transparency of the widget background ImageView (requires Froyo or greater), e.g.
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) 以获得最大的兼容性.
I've used both methods and recommend (2) for maximum compatibility.
这篇关于在小部件中动态设置 ImageView 的圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!