Pham Lai Pham Lai - 4 months ago 85
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

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);

Source: http://www.widecodes.com/fxSzNWPeVX/android-image-view-top-crop.html


Since I so that people are requesting more scaleTypes in ImageView: https://code.google.com/p/android/issues/detail?id=58468 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: https://github.com/jonathanrz/PhotoView/ and add the aar to your project manually.

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