0

在小部件的配置阶段,我想更新作为图层列表一部分的位图。由于 RemoteViews 和 Widgets 的所有限制,我发现自己无法实现这一点。既没有手动创建新的 LayerDrawable,也没有检索和更新 drawable。你有想法吗?

背景:小部件显示照片以及其他一些信息。即使是方形、横向或纵向的,照片也应该有良好的框架和比例。

图层列表:

<?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>

可绘制的@drawable/photo_border 是一个 9-patch 位图。

布局中的相应部分:

<!-- ... -->
<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>
<!-- ... -->

感谢您分享您的想法。

4

1 回答 1

0

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>
于 2012-12-14T11:53:04.020 回答