Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView fit without stretching the image

Tags:

is it possible that my ImageView will fit to all screen sizes without the image look stretched?

i tried all scale types changing it to background and src with fill_parent > wrap_content

but still. if the image is not smaller it just looks stretched..

example:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"
    android:adjustViewBounds="true"
    android:background="@drawable/background" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:background="@drawable/background" />

these 2 fits the width but the image is stretched.. but when i change it to src the image is just centered and small.

like image 882
NoobMe Avatar asked Dec 18 '12 02:12

NoobMe


People also ask

How do I fit a div image without stretching it?

Those images on the site you linked to are actual size, so the simple answer is just to resize the image. You can't really do this without "stretching" the image if the image happens to be less than 300px wide or tall, but you can do it without changing the ratio, which is what I think you mean.

How do I resize an image without stretching it CSS?

The trick is to put the image into a containing block element, eg a DIV. Once inside set the width of the image to 100%, this will instruct the browser to fit the image width flush with the left and right edges of the DIV.


1 Answers

There is no built-in scale type that allows the ImageView to automatically upscale the image and keep its aspect ratio intact. The only scale type that does upscaling is fitXY, which won't respect the aspect ratio, as you found out yourself too.

That being said, this topic has already been visited more than once. Have a look at some of the proposed suggestions for similar questions:

  • ImageView one dimension to fit free space and second evaluate to keep aspect ration
  • Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
  • Resizing ImageView to fit to aspect ratio

I'm sure'll be heaps more, so it might worth using the search box at the top.

like image 197
MH. Avatar answered Sep 21 '22 10:09

MH.