Call setImageDrawable from RemoteViews

醉酒当歌 提交于 2019-12-10 10:33:46

问题


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


来源:https://stackoverflow.com/questions/8113133/call-setimagedrawable-from-remoteviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!