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?
setMinimumWidth(width); imgView. setMinimumHeight(height); imgView. setMaxWidth(width); imgView. setMaxHeight(height);
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.
You can use this segment to set scale type for Image View programatically..
imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Hope it helps...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With