Setting Gradientdrawable Through Remoteview
Here is what I want to do: I have a widget and I want to set its background depending upon users choice of colors. It has to be a gradient. The backgound is to be set by setting ba
Solution 1:
Tried ImageView as a background (before LinearLayout). It does not provide proper margin to widget. Since the widget text is dynamic, sometimes it goes out of the imageView which is not what I want
I'm not entirely sure what you mean, but if you use a FrameLayout / RelativeLayout for your root layout, and then put the ImageView inside with fill parent, your image should be exactly the size of your widget.
<FrameLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_margin="6dp" ><ImageViewandroid:id="@+id/widgetBg"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scaleType="fitXY" />
// Other views
</FrameLayout>
Also, this is what I'm doing to dynamically change the color & alpha of a rounded corner gradient background. Then use setImageViewBitmap( ) to apply to the imageview. Probably there is a better way.
publicstatic Bitmap getBackground(int bgColor, int width, int height, Context context) {
try {
// convert to HSV to lighten and darkenint alpha = Color.alpha(bgColor);
float[] hsv = newfloat[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,newint[] { darker, lighter});
gd.setGradientType(GradientDrawable.RECTANGLE);
// set corner size
gd.setCornerRadii(newfloat[] {4,4,4,4,4,4,4,4});
// get density to scale bitmap for devicefloat 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();
returnnull;
}
}
Post a Comment for "Setting Gradientdrawable Through Remoteview"