下面是我想做的事情:我有一个小部件,我想根据用户的颜色选择来设置它的背景。它必须是一个梯度。背景将通过设置linearLayout的背景来设置。为了测试,我做了一个假人背景,如:
remoteViews.setInt(R.id.layout, "setBackgroundResource", R.drawable.widget_background);我已经看到了这个问题:Call setImageDrawable from RemoteViews,但我无法理解如何实现。我甚至找不到上面提到的setXYZ()。到目前为止,我一直在尝试这样做:
现在我完全糊涂了,被困住了。有人能帮助(,可能是更多的代码,更少的链接)吗?另外,请不要像已经问过的那样结束这个问题。
发布于 2013-01-12 08:24:48
尝试将ImageView作为背景(在LinearLayout之前)。它没有为小部件提供适当的空间。由于小部件文本是动态的,有时它会离开imageView,这不是我想要的。
我不太清楚您的意思,但是如果您使用FrameLayout / RelativeLayout作为根布局,然后将ImageView与fill父本放在一起,则您的映像应该与您的小部件的大小完全相同。
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="6dp" >
<ImageView
android:id="@+id/widgetBg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
// Other views
</FrameLayout>此外,这也是我所做的动态改变颜色&α的圆角梯度背景。然后使用setImageViewBitmap( )对图像视图进行应用。也许还有更好的办法。
public static Bitmap getBackground(int bgColor, int width, int height, Context context) {
try {
// convert to HSV to lighten and darken
int alpha = Color.alpha(bgColor);
float[] hsv = new float[3];
Color.colorToHSV(bgColor, hsv);
hsv[2] -= .1;
int darker = Color.HSVToColor(alpha, hsv);
hsv[2] += .3;
int lighter = Color.HSVToColor(alpha, hsv);
// create gradient useng lighter and darker colors
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,new int[] { darker, lighter});
gd.setGradientType(GradientDrawable.RECTANGLE);
// set corner size
gd.setCornerRadii(new float[] {4,4,4,4,4,4,4,4});
// get density to scale bitmap for device
float dp = context.getResources().getDisplayMetrics().density;
// create bitmap based on width and height of widget
Bitmap bitmap = Bitmap.createBitmap(Math.round(width * dp), Math.round(height * dp),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
gd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
gd.draw(canvas);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}https://stackoverflow.com/questions/12464535
复制相似问题