Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RemoteViews, how to set ScaleType of an ImageView inside a widget?

I am developing a widget with an image on the rigth side, which can be choosen by the user in one of the settings screen. The image can then be set from the user with the ImageView.setScaleType("CENTER"). That works. The URL of the image is then stored in the preferences as URL, and also as Base64 encoded String of the Bitmap (cause I want to shrink the image and the user can rotate it in the settings)

In the widget, I load the image. That works fine. With the URI and with the Bitmap too. The ScaleType of the ImageView is set as well to a fixed value in the Layout, works too.

But how can I define the ScaleType of the ImageView in the widget programmatically? Cause I want to set the ScaleType to the value, the user has choosen in the settings. With RemoteViews, we cannot get the ImageView...

I tried:


myRemoteViews.setString(R.id.myImage, "setScaleType", "CENTER");


The logfile says: The function setScaleType is not possible for ImageView.

Does someone know how to make this definition inside the AppWidget?

like image 692
user1013443 Avatar asked Oct 25 '11 20:10

user1013443


People also ask

How do I make my android picture fit the screen?

setMinimumWidth(width); imgView. setMinimumHeight(height); imgView. setMaxWidth(width); imgView. setMaxHeight(height);

What is adjust view bounds in android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it. android:resizeMode—The resizeMode attribute is used to make a control resizable so we can resize it horizontally, vertically, or around both axes.


2 Answers

You can use this segment to set scale type for Image View programatically..

 imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

Hope it helps...

like image 113
ShivRaj Avatar answered Oct 11 '22 16:10

ShivRaj


If you look at the source code of ImageView, you can see the setScaleType method lacks the @android.view.RemotableViewMethod annotation, so it can't be called through the RemoteViews interface.

There's a workaround for this limitation, but it's not pretty: define a different layout XML for each ScaleType you want, and choose between them just before you create the RemoteViews. If your layout is complex, or you have more than one view to apply this workaround to, you might want to use RemoteViews.addView to add the particular ImageView layout inside the main layout.

I'm using this workaround (with addView) for appwidgets in my app Showr. The problem with this method is that the stock launcher has a bug: if you change the scale type of an existing appwidget by creating a new RemoteViews and calling addView to add a different ImageView layout to it, it doesn't always realise the layout has changed.

like image 37
Dan Hulme Avatar answered Oct 11 '22 16:10

Dan Hulme