CQM
CQM

Reputation: 44278

android centerCrop for larger screens, where ImageView width matches parent

I use android:scaleType="centerCrop" throughout my app so that images of unknown aspect ratios and resolutions display in a uniform and scaled way in my ImageViews.

the centerCrop algorithm scales an image from the center of the view, outside of the bounds of the imageview.

With phones in portrait mode, this is great. The phone form factor aspect ratio is ideal for centerCrop. But in landscape mode and larger screens centerCrop does not typically stick to a safe area of how images are composed. The subjects heads are cut off usually

is there an additional scaleType I can use to account for this dynamically, even if I start detecting resolutions and making alternate layouts for them, what is the alternative to centerCrop? maintain the same kind of scaling but not losing the subject's focus

Upvotes: 0

Views: 548

Answers (1)

weston
weston

Reputation: 54801

You can supply your own matrix and scale it however you like so you can specify the centre you want.

Set the scale type to matrix then use setImageMatrix

imageView.setScaleType(ScaleType.MATRIX);
Matrix m = new Matrix();
float scale = 2;
m.setScale(scale, scale, 50, 100); //scale by 2x around the point 50, 100
imageView.setImageMatrix(m);

The downside is you need to do the maths to work out the correct scale so that it fits when scaled from that point. Keep it the same in X and Y as above and it will keep the aspect ratio.

You can start with the code from ImageView that handles CENTER http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/ImageView.java then just modify the pivot point to suit.

Upvotes: 0

Related Questions