-->

Call setImageDrawable from RemoteViews

2019-05-31 05:45发布

问题:

I have done this in an Activity and it works perfectly.

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

Now I want to do the same thing in a widget (inside onUpdate) and I have to use RemoteViews to access the image.

How do I call the setImageDrawable of an ImageView from RemoteViews? All the remote views methods seems to take a Bitmap.

回答1:

RemoteViews are built from XML resource descriptors. You can not use code to build them.

You need to do something like this:

Create a layout:

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

Then define a new shape drawable named myshapedrawable.xml (in res/drawables folder):

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


回答2:

You can change color of ImageView image in "RemoteViews" by doing this:

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


回答3:

after modify drawable :



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