Tuesday, 18 June 2019

Image scaling and positioning in ImageView inside AppWidget layout

When I set the content of an ImageView (N.B. inside an AppWidget layout) to a bitmap that is smaller than the physical pixel size of the ImageView, I'm sure that in the past (not sure how long ago) the bitmap would just get scaled up to fit... in my case the smaller image is exactly the same aspect ratio as the ImageView so this is exactly the effect I want.

But recently I noticed that this is no longer happening... the bitmap sits in the top left corner. I've tried setting various attributes in the XML layout, but nothing has any effect.

E.g. I've tried:

android:scaleType="fitCenter"

android:scaleType="centerInside"

android:layout_gravity="center"

But still the image sits in the ImageView flush with the top left corner, with gaps to right and bottom.

XML (ImageView part):

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView_port"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

Code to put image content URI in appwidget:

remoteViews.setImageViewUri(imageViewId, uri);
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, remoteViews);

How it looks for image with size that matches the widget imageview:

enter image description here

How it looks for an image that is smaller than the widget imageview:

enter image description here

I want the smaller image to be scaled up to fit the ImageView (it's the same aspect ratio, so it will fit perfectly).



from Image scaling and positioning in ImageView inside AppWidget layout

No comments:

Post a Comment