Call setImageDrawable from RemoteViews



我已经在一个Activity中这样做了,效果很好。

ImageView myImage = (ImageView) findViewById(R.id.myImageView);
ShapeDrawable mDrawable;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
mDrawable.setIntrinsicWidth(width);
mDrawable.setIntrinsicHeight(height);
myImage.setImageDrawable(mDrawable);

现在我想在小部件中做同样的事情(在onUpdate中),我必须使用RemoteViews来访问图像。

如何从RemoteViews调用ImageViewsetImageDrawable ?所有的远程视图方法似乎都需要一个Bitmap .

你可以通过这样做来改变"RemoteViews"中的ImageView图像的颜色:

remoteviews.setInt(viewid, "setColorFilter", color);

RemoteViews是由XML资源描述符构建的。你不能用代码来构建它们。

你需要这样做:

创建布局:

<ImageView android:src="@drawable/myshapedrawable">
</ImageView>

然后定义一个新的可绘制形状命名为myshapedrawable.xml(在res/drawables文件夹中):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">
    <size android:width="200" android:height="100"/>
    <solid android:color="0xff74AC23"/>
</shape>
after modify drawable :

 Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
 remoteViews.setImageViewBitmap(viewId, bitmap) .

最新更新