Nick
Nick

Reputation: 3461

Android ImageView scaleType

I have an image to be used a background in activity:

enter image description here

I want this image to fit screen by its height. It means that for wide-screen smartphones, I want my image to be fit by height and centered:

enter image description here

and for square-screen smartphones, I want my image to be cut:

enter image description here

I use ImageView for the image. What scaleType should I use? Looking at the second figure, I'd say to use android:scaleType="centerInside". But looking at the third, I'd say to use android:scaleType="centerCrop". What is correct?

Upvotes: 1

Views: 1388

Answers (4)

mertsimsek
mertsimsek

Reputation: 276

When you fit one side of image to background, you will face with two problems, first one is screen width is bigger than image's width or screen height is bigger than image height.So you will have empty space.

If you do not want to face with resolution problem and you want to fit both side of image to background, you need to use centerCROP.But as i said, if one side of image is not enough to fit background, image gets bigger till it will be filled.

Upvotes: 1

Yehor Nemov
Yehor Nemov

Reputation: 927

Use centerCrop because centerInside doesn't scale an image in a view and you have to create the image with appropriate height to achieve wide-screened smartphones background filling. Or alternative you could use fitCenter to get uniformly scaled image by both axes which fills the all background.

Upvotes: 0

gtsouk
gtsouk

Reputation: 5273

You can have two layouts, one for each of your configurations. You can then load the proper one at the activity's onCreate() call.

Upvotes: 1

Dan Harms
Dan Harms

Reputation: 4840

Your evaluation of the different scaleType's is correct. If you want the whole image to be visible, use "centerInside", or if you want to fill the whole view then use centerCrop.

To use a mix of both, you can set the scaleType in your onCreate() method. Based on the behavior you want to have, you can check the orientation or size of the screen and set the appropriate choice.

imageView.setScaleType(ScaleType.CENTER_INSIDE); // or ScaleType.CENTER_CROP

Upvotes: 1

Related Questions