From 410b363a649a0e02edd7dc1bfd504c927cbb3cd8 Mon Sep 17 00:00:00 2001 From: Bo Date: Fri, 12 Oct 2018 16:57:17 +0800 Subject: [PATCH] Fix multi-touch issue when a secondary pointer lifts This is to fix multi-touch issue seen on some OEM devices i.e. OPPO, where the image might pan abruptly when the user lifts a secondary pointer. This is mainly caused by using the cached pointer index from last event, which is not guaranteed to point to the same pointer across events. --- .../photoview/CustomGestureDetector.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/photoview/src/main/java/com/github/chrisbanes/photoview/CustomGestureDetector.java b/photoview/src/main/java/com/github/chrisbanes/photoview/CustomGestureDetector.java index 0c2c6049..2beea242 100755 --- a/photoview/src/main/java/com/github/chrisbanes/photoview/CustomGestureDetector.java +++ b/photoview/src/main/java/com/github/chrisbanes/photoview/CustomGestureDetector.java @@ -27,9 +27,10 @@ class CustomGestureDetector { private static final int INVALID_POINTER_ID = -1; + private static final int INVALID_INDEX = -1; private int mActivePointerId = INVALID_POINTER_ID; - private int mActivePointerIndex = 0; + private final ScaleGestureDetector mDetector; private VelocityTracker mVelocityTracker; @@ -74,17 +75,31 @@ public void onScaleEnd(ScaleGestureDetector detector) { mDetector = new ScaleGestureDetector(context, mScaleListener); } + private int getActivePointerIndex(MotionEvent ev) { + return mActivePointerId == INVALID_POINTER_ID ? INVALID_INDEX : ev.findPointerIndex(mActivePointerId); + } + private float getActiveX(MotionEvent ev) { + int index = getActivePointerIndex(ev); + if (index == INVALID_INDEX) { + return ev.getX(); + } + try { - return ev.getX(mActivePointerIndex); + return ev.getX(index); } catch (Exception e) { return ev.getX(); } } private float getActiveY(MotionEvent ev) { + int index = getActivePointerIndex(ev); + if (index == INVALID_INDEX) { + return ev.getY(); + } + try { - return ev.getY(mActivePointerIndex); + return ev.getY(index); } catch (Exception e) { return ev.getY(); } @@ -153,7 +168,6 @@ private boolean processTouchEvent(MotionEvent ev) { } break; case MotionEvent.ACTION_UP: - mActivePointerId = INVALID_POINTER_ID; if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = getActiveX(ev); @@ -175,6 +189,8 @@ private boolean processTouchEvent(MotionEvent ev) { } } + mActivePointerId = INVALID_POINTER_ID; + // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.recycle(); @@ -194,10 +210,6 @@ private boolean processTouchEvent(MotionEvent ev) { } break; } - - mActivePointerIndex = ev - .findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId - : 0); return true; } }