20

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.

sabadow
  • 5,095
  • 3
  • 34
  • 51
NoobMe
  • 544
  • 2
  • 6
  • 26
  • You are asking for something that is most obviously impossible. Unless of course, the image and the screen/view you want to cram it into happen to have identical aspect ratios. – mathheadinclouds Dec 21 '14 at 02:06

2 Answers2

32

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:

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

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • upvoted thanks MH... arghh. seems like its impossible. =_= to fix in xml~ adjusting the width to fit the screen really stretches the image – NoobMe Dec 18 '12 at 03:19
  • 1
    i have accepted your answer. i found out in another topic that you cant do this only on xml.. you must fix it in java.. thank you~ – NoobMe Dec 18 '12 at 09:24
13

You can do it by just adding just one attributes to ImageView

android:adjustViewBounds="true"
Juned Khatri
  • 451
  • 5
  • 8
  • Thanks! I was searching for the right answer for so long using the scale type. but this one line made it really easy. Thanks a lot – Riddhi Shah Jul 26 '19 at 05:46