Pham Lai Pham Lai - 1 year ago 222
Android Question

How to TOP CROP Image using PhotoView Android?

I have 1 ImageView with layout_width & layout_height is match_parent, two of them is screen width and height size. I need display photo to fill full width and when image is higher than ImageView height, it can be scroll up and down. I'm using PhotoView library to zoom, scroll, tap... now i'm using centerCrop scale type but it is not solution i need. It show a part of center image and i need scroll up to view from beginning.

I tried lot of custom view extended ImageView to display Top Crop style but when i touch to image, it changed to default PhotoView scale type. It only work when not use PhotoViewAttacher

How i can make a Top Crop and scroll + zoom + touch... image by using PhotoView library ?

Answer Source

You can achieve a TopCrop scale changing the CENTER_CROP scale under updateBaseMatrix() in PhotoViewAttacher of Chris Banes`s PhotoView.

Here is the code:

else if (mScaleType == ScaleType.CENTER_CROP) {
   float scale = Math.max(widthScale, heightScale);
    mBaseMatrix.postScale(scale, scale);
    //Changed dy = 0 for top crop
    mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F, 0);


Since I so that people are requesting more scaleTypes in ImageView: I added custom scale types to Chris Banes`s PhotoView.

I send a PR to him. Since I don't know if he will accept the PR(or when), you can get the project in my github: and add the aar to your project manually.

You can see an example of usage in CropTypeActivity in the sample project inside the library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download