Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView: fill horizontal maintaining aspect ratio

I need an ImageView scale it image until fill the parent horizontally.

If the imageview background is red, and the rest of the content (below the image) is the green I'm looking for the result shown by the Picture 1. That result is obtained automatically if the image width is higher than the screen width.

But if is a small picture like in the picture 2. the best result I can get is the picture 3 (setting the ImageView width and height to fill_parent and the scaleType to FitStart

The picture 4 is obtained setting height= wrap_content, width= fill_parent, scaleType= CenterCrop. It should scale vertically to show the entire image, but as the scaleType says it crops it.

Any ideas for getting the picture 1 even if the image is small?

Will grant a bounty of 50 to a working answer

enter image description here

like image 829
Addev Avatar asked Mar 08 '12 19:03

Addev


1 Answers

I found an easy solution that works really good for me. Set width and height as you wish, e.g.:

android:layout_width="fill_parent"
android:layout_height="wrap_content"

Then you also set this setting:

android:adjustViewBounds="true"

"adjustViewBounds" defaults to false (which I find odd) but setting it makes it easy to fill the image in the imageview.

like image 89
mikeesouth Avatar answered Oct 06 '22 20:10

mikeesouth