Question

During the configuration phase of a widget, I would like to update a bitmap that is part of a layer-list. With all the restrictions of RemoteViews and Widgets, I see myself unable achieving this. Neither creating a new LayerDrawable manually nor retrieving and updating the drawable worked out. Do you have an idea?

Background: The Widget displays a Photo along with some other information. The photo should be framed and scale nicely, even if its sqare, landscape or portrait.

Layer-List:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/photo_01"></item>
    <item android:drawable="@drawable/photo_border"></item>
</layer-list>

The drawable @drawable/photo_border is a 9-patch bitmap.

The corresponding part from the layout:

<!-- ... -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="20dp" >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:padding="4dp"
        android:scaleType="fitCenter"
        android:src="@drawable/framed_placeholder" />

</RelativeLayout>
<!-- ... -->

Thank You for sharing your thoughts.

Was it helpful?

Solution

While sleeping the problem over, I found, that I could probably just render the Bitmap manually and update the imageView. Here's the code. It's not nice, but get's the job done.

This probably can still be improved with better handling of density, etc.

Bitmap bmp = BitmapFactory.decodeFile(sdCard.getAbsolutePath() + pathName);
Bitmap cBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(cBitmap);
c.drawBitmap(bmp, 0, 0, null);
NinePatchDrawable photo_border = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.photo_border);
photo_border.setBounds(0, 0, bmp.getWidth(), bmp.getHeight());
photo_border.draw(c);

views.setImageViewBitmap(id.photo, cBitmap);

One thing to be aware of: When the phone boots, the widgets may receive an Update event, when the sd card is not mounted. You should either wait until it is, or skip the configuration and add an intent-filter (that's what I did)

<intent-filter>
    <action android:name="android.intent.action.MEDIA_MOUNTED"/>
    <data android:scheme="file"></data>
</intent-filter>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top