Rounded corners on dynamicly set ImageView in widget?

android, imageview, widget

Solution

There are 2 methods of doing this:

- Create a bitmap with rounded/square corners (using your existing method) that is roughly the size of the widget you want. This has risk of the bitmap distorting if the user resizes the widget or uses it on a device with some screen resolution/DPI you have not taken into consideration

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);

I've used both methods and recommend (2) for maximum compatibility.

Problem

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-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; } } ``` Then ``` 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?

Original source