4

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?

timothyjc
  • 2,188
  • 3
  • 29
  • 54

2 Answers2

12

There are 2 methods of doing this:

  1. 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
  2. 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.

roflharrison
  • 2,300
  • 23
  • 26
  • Hi! I just asked a question here http://stackoverflow.com/questions/25877515/appwidget-image-with-rounded-corners that's very related to this question. Any insight would be great! – Will Thomson Sep 16 '14 at 20:07
0

Too late for the answer, but maybe it will be useful to someone. Recently I also figured out how to make rounded corners in RemoteViews. In my case, I needed to display the image by url and make it round cornenrs. Unfortunately, remoteViews.setImageViewResource and remoteViews.setInt didn't work right.

I solved the problem with Glide:

val notificationTarget = NotificationTarget(
    baseContext,
    R.id.id_of_your_image_view,
    expandedView,
    notification,
    NOTIFICATION_ID
)
Glide.with(baseContext.applicationContext)
    .asBitmap()
    .load("url of the image")
    .circleCrop() // the method that rounds the corners
    .into(notificationTarget)
Anonymous
  • 301
  • 3
  • 8