Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView one dimension to fit free space and second evaluate to keep aspect ration

I need something like<img width="100%" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep aspect ratio.

Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">

Useandroid:scaleType="fitCenter"do with image what I want, but only with image, doesn't change height of View itself. When I set booth dimensions to match_parent I will get what I want, but only if I have only one image on the screen. But I need some texts under image.

It's only way create child of ImageView? For exampleandroid:adjustViewBounds="true"does't do anything for me.

like image 917
ATom Avatar asked Jun 01 '11 13:06

ATom


People also ask

What is adjustViewBounds?

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.19-Dec-2012.


1 Answers

I found where is problem. Default implementation of ImageView can't enlarge image. If image is bigger than screen width android:adjustViewBounds="true" works correctly and adjust height to keep aspect ratio of image. But if image is smaller that screen width it don't upscale it.

This is cause by this code

if (newHeight <= heightSize) {
    heightSize = newHeight;
} 

in ImageView class (on line 688, API 10)

I made special child of ImageView which allow enlarge image to fit width.

Here is: https://gist.github.com/tprochazka/5486822

like image 72
ATom Avatar answered Oct 19 '22 23:10

ATom