matabii / scale-imageview-android

Add pinch-in and pinch-out function to Android ImageVIew.
MIT License
91 stars 49 forks source link

Compatibility with a ViewPager #1

Open jvnn opened 11 years ago

jvnn commented 11 years ago

First of all, thanks for the nice widget! There is a problem though when using the ScaleImageView with a ViewPager, weird things happen because the pager feeds unexpected values to the setFrame method. The problems is fixed in the code below.

protected boolean setFrame(int l, int t, int r, int b) {
    mWidth = r - l;
    mHeight = b - t;

    mMatrix.reset();

    /* When using this with a ViewPager, r can be for example -960 while l is -1440.
     * This is totally uncool, so we should make sure that the values are within
     * the screen size limits when used for other calculations. 
     */
    int r_norm = r - l;
    mScale = (float) r_norm / (float) mIntrinsicWidth;
    int paddingHeight = 0;
    int paddingWidth = 0;
    // scaling vertical
    if (mScale * mIntrinsicHeight > mHeight) {
        mScale = (float) mHeight / (float) mIntrinsicHeight;
        mMatrix.postScale(mScale, mScale);
        paddingWidth = (r_norm - mWidth) / 2;
        paddingHeight = 0;
        // scaling horizontal
    } else {
        mMatrix.postScale(mScale, mScale);
        paddingHeight = (b - mHeight) / 2;
        paddingWidth = 0;
    }
    mMatrix.postTranslate(paddingWidth, paddingHeight);

    setImageMatrix(mMatrix);
    mMinScale = mScale;
    zoomTo(mScale, mWidth / 2, mHeight / 2);
    cutting();
    return super.setFrame(l, t, r, b);
}
matabii commented 11 years ago

Thank you for report!

I have also tried. It discovered not moving satisfactorily, when ScaleImageView had arranged on ViewPager. ViewPager hook the flick of left and right. So when enlarge an image, Can't scroll left and right.

I'll try looking for a solution. Thank you.

jvnn commented 11 years ago

Hi,

The solution is already included in my first message, that code works at least for me. :)

The main change is this line: int r_norm = r - l;

And then using r_norm instead of r for later calculations (except when calling super.setFrame).

EDIT: Oh, I read your comment again, and realised that you also mentioned zooming in and then scrolling. I had to disable swiping on the ViewPager completely so that it doesn't interfere with zooming and panning the image in the ScaleImageView. There is a good piece of code in stackoverflow: http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s