summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/PagerAwareImageMatrixTouchHandler.java
blob: 46609f8fb4df857b301140c4ce15c10c68c3260f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.fox.ttrss;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.bogdwellers.pinchtozoom.ImageMatrixTouchHandler;

// Based on https://github.com/martinwithaar/PinchToZoom/blob/master/pinchtozoom/src/main/java/com/bogdwellers/pinchtozoom/view/ImageViewPager.java
// mLastMotionX is a simplified version of ViewPager's MotionEvent logic
public class PagerAwareImageMatrixTouchHandler extends ImageMatrixTouchHandler {
    /**
     * NOT Thread safe! (But it all happens on the UI thread anyway)
     */
    private static final float[] VALUES = new float[9];

    private static final float SCALE_THRESHOLD = 1.2f;

    private float mLastMotionX;

    public PagerAwareImageMatrixTouchHandler(Context context) {
        super(context);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        super.onTouch(view, event);

        if (event.getPointerCount() > 1) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            return true;
        }

        if (event.getAction() != MotionEvent.ACTION_MOVE)
        {
            return true;
        }

        float x = event.getX(0);
        float dx = x - mLastMotionX;
        mLastMotionX = x;

        ImageView iv = (ImageView) view;
        Drawable drawable = iv.getDrawable();
        if (drawable != null) {
            float vw = iv.getWidth();
            float vh = iv.getHeight();
            float dw = drawable.getIntrinsicWidth();
            float dh = drawable.getIntrinsicHeight();

            Matrix matrix = iv.getImageMatrix();
            matrix.getValues(VALUES);
            float tx = VALUES[Matrix.MTRANS_X] + dx;
            float sdw = dw * VALUES[Matrix.MSCALE_X];

            boolean blockScroll = VALUES[Matrix.MSCALE_X] / centerInsideScale(vw, vh, dw, dh) > SCALE_THRESHOLD && !translationExceedsBoundary(tx, vw, sdw) && sdw > vw; // Assumes x-y scales are equal
            view.getParent().requestDisallowInterceptTouchEvent(blockScroll);
        }

        return true;
    }

    /**
     * <p>Returns the scale ratio between view and drawable for the longest side.</p>
     *
     * @param vw
     * @param vh
     * @param dw
     * @param dh
     * @return
     */
    public static final float centerInsideScale(float vw, float vh, float dw, float dh) {
        return vw / vh <= dw / dh ? vw / dw : vh / dh;
    }

    /**
     * <p>Determines whether a translation makes the view exceed the boundary of a drawable.</p>
     *
     * @param tx
     * @param vw
     * @param dw
     * @return
     */
    public static final boolean translationExceedsBoundary(float tx, float vw, float dw) {
        return dw >= vw && (tx > 0 || tx < vw - dw);
    }
}