20

Things looked quite simple first but in the end the result is not good. I have an image which has a width larger than screen's width. So I need to scale it down in my imageview. I looked over the ScaleType options and tried them all but none is ok. First "center" only displays the image centered on the layout, no scaling done. "fitCenter" scales the image to fit in my layout but has a major drawback: the height of the item remains as it would have the large image in it. Take a look at the second screen in the attached image. How can I force the list item, to reduce its height to wrap both the text and the image ?

enter image description here

Alin
  • 14,809
  • 40
  • 129
  • 218

4 Answers4

51

Use the scaletype which seems best to you ( I guess you like what you see with fitCenter). The additional thing that you must do is

android:adjustViewBounds="true"

in your ImageView.

or you could go with FitXY but sometimes the result is not exactly what you want.

wojciii
  • 4,253
  • 1
  • 30
  • 39
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • Yes, this was the missing property. Works fine with adjustViewBounds and fitCenter – Alin Apr 12 '12 at 14:10
  • I have image of 1000*800 but I cant get the full ImageView using any of these properties.. if any combination lies with it. pls help me out – Gnanam R Apr 26 '13 at 11:25
  • Will need more info, like whether the imageview is in a layout which is smaller that your image size, and how is the image being set. – Shubhayu Apr 26 '13 at 12:53
  • @Shubhayu: I have a problem that wanna stretch my image to my Imageview and keep it ascept ratio. I wanna use such a image on for example for title image that must shown in all size screens. – Mahdi Jun 30 '14 at 09:48
  • @Kenji If you want to stretch the image, then you must make sure that your ImageView is of the same aspect ratio as that of your src image and then use fitXY in scaleType. If you don't maintain the aspect ratio then any of the other scaleTypes will lead to cropping of the src image or empty space showing your imageview's background. – Shubhayu Jul 02 '14 at 10:47
3

use android:scaleType="fitXY"

Nishant Rajput
  • 2,053
  • 16
  • 28
2

Could you use FitXY? This would work if you knew the size of the area you were putting the image into. CentreInside may also work, I've used this to scale down images, but I think it depends if you've control of the size of the bounding layout element.

Andrew
  • 7,548
  • 7
  • 50
  • 72
0

You either need to set android:height = "wrap_content" on your outer container, or set static height of TextBox and give ImageView android:weight = "1" so that the ImageView takes remaining space in container.

Sasa
  • 32
  • 3