views:

1240

answers:

3

Hi,

In android, I defined an ImageView's layoutWidth to be 'fill_parent' (which takes up the full width of the phone). My question is if the Image i put to ImageView is bigger than the layoutWidth, android will scale it, right?

But what about the height? when android scale it, will it keeps the aspect ratio?

What I find out is, there is some 'white space' at the top/bottom of the ImageView when android scales an image which is bigger than the ImageView. Is that true? If yes, how can I eliminate that white spaces?

Thank you.

A: 

Take a look at ImageView.ScaleType to control and understand the way resizing happens in an ImageView. When the image is resized(while maintaining its aspect ratio), chances are that either the image's height or width becomes smaller than ImageViews dimensions.

Samuh
+5  A: 

1) Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)

2) There isn't "white space", it's filled with transparent pixels. If you don't want even those, you could simply put your layout_width="fill_parent" and layout_height="wrap_content".

Then as Samuh wrote, you can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.

Steve H
+10 for taking time to detail :)
Samuh
Thanks. But what if I have an ImageView, and I set the image by using imageView.setImageBitmap()?
michael
That's the same thing, just done in code rather than XML. `setImageBitmap` is the same as `android:src="..."` and `setBackground...` is `android:background="..."`
Steve H
+7  A: 

See "android:adjustViewBounds"

Gratzi
This is the correct fix for the issue. We were having this on our image view, where the height included all of this extra whitespace. It isn't "transparent pixels", since we had fill_parent for width, and wrap_content for height. If you don't have adjustViewBounds=true, then you get the extra whitespace. After setting that to true, our issue went away. Thanks!
christophercotton
thanks, this and setting it to src instead of background worked perfectly!
Cameron