14

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.

ATom
  • 15,960
  • 6
  • 46
  • 50

1 Answers1

18

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

ATom
  • 15,960
  • 6
  • 46
  • 50
  • I'm using your code, in a listview, with universal image loader, and I noticed, that with big images, it stalls the ui a lot. Any ideas how to make it more efficient? – Tamas Jul 25 '13 at 12:42
  • My modification is for fit small images to fit bigger IamgeView area, for example if user run the app on tablet or new device with higher screen density. If you want to use big images in the ListView you should downscale it to fit exactly the ImageView size and cache this size in LRU disk cache. – ATom Jul 29 '13 at 20:27