|
| 1 | +package org.fox.ttrss; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Matrix; |
| 5 | +import android.graphics.drawable.Drawable; |
| 6 | +import android.view.MotionEvent; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.ImageView; |
| 9 | + |
| 10 | +import com.bogdwellers.pinchtozoom.ImageMatrixTouchHandler; |
| 11 | + |
| 12 | +// Based on https://github.com/martinwithaar/PinchToZoom/blob/master/pinchtozoom/src/main/java/com/bogdwellers/pinchtozoom/view/ImageViewPager.java |
| 13 | +// mLastMotionX is a simplified version of ViewPager's MotionEvent logic |
| 14 | +public class PagerAwareImageMatrixTouchHandler extends ImageMatrixTouchHandler { |
| 15 | + /** |
| 16 | + * NOT Thread safe! (But it all happens on the UI thread anyway) |
| 17 | + */ |
| 18 | + private static final float[] VALUES = new float[9]; |
| 19 | + |
| 20 | + private static final float SCALE_THRESHOLD = 1.2f; |
| 21 | + |
| 22 | + private float mLastMotionX; |
| 23 | + |
| 24 | + public PagerAwareImageMatrixTouchHandler(Context context) { |
| 25 | + super(context); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean onTouch(View view, MotionEvent event) { |
| 30 | + super.onTouch(view, event); |
| 31 | + |
| 32 | + if (event.getPointerCount() > 1) { |
| 33 | + view.getParent().requestDisallowInterceptTouchEvent(true); |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + if (event.getAction() != MotionEvent.ACTION_MOVE) |
| 38 | + { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + float x = event.getX(0); |
| 43 | + float dx = x - mLastMotionX; |
| 44 | + mLastMotionX = x; |
| 45 | + |
| 46 | + ImageView iv = (ImageView) view; |
| 47 | + Drawable drawable = iv.getDrawable(); |
| 48 | + if (drawable != null) { |
| 49 | + float vw = iv.getWidth(); |
| 50 | + float vh = iv.getHeight(); |
| 51 | + float dw = drawable.getIntrinsicWidth(); |
| 52 | + float dh = drawable.getIntrinsicHeight(); |
| 53 | + |
| 54 | + Matrix matrix = iv.getImageMatrix(); |
| 55 | + matrix.getValues(VALUES); |
| 56 | + float tx = VALUES[Matrix.MTRANS_X] + dx; |
| 57 | + float sdw = dw * VALUES[Matrix.MSCALE_X]; |
| 58 | + |
| 59 | + 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 |
| 60 | + view.getParent().requestDisallowInterceptTouchEvent(blockScroll); |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * <p>Returns the scale ratio between view and drawable for the longest side.</p> |
| 68 | + * |
| 69 | + * @param vw |
| 70 | + * @param vh |
| 71 | + * @param dw |
| 72 | + * @param dh |
| 73 | + * @return |
| 74 | + */ |
| 75 | + public static final float centerInsideScale(float vw, float vh, float dw, float dh) { |
| 76 | + return vw / vh <= dw / dh ? vw / dw : vh / dh; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * <p>Determines whether a translation makes the view exceed the boundary of a drawable.</p> |
| 81 | + * |
| 82 | + * @param tx |
| 83 | + * @param vw |
| 84 | + * @param dw |
| 85 | + * @return |
| 86 | + */ |
| 87 | + public static final boolean translationExceedsBoundary(float tx, float vw, float dw) { |
| 88 | + return dw >= vw && (tx > 0 || tx < vw - dw); |
| 89 | + } |
| 90 | +} |
0 commit comments