Skip to content

Commit d207eff

Browse files
committed
fix: touch velocities should be ignored when a view receives the ACTION_CANCEL event
1 parent b87b1a6 commit d207eff

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

gallery-viewer/src/main/java/com/liuzhenlin/galleryviewer/GestureImageView.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,18 +517,20 @@ public boolean onTouchEvent(MotionEvent event) {
517517
}
518518

519519
// No scaling is needed below
520-
mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
521-
final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
522-
final float vy = mVelocityTracker.getYVelocity(mActivePointerId);
523-
// If one of the velocities is not less than our minimum fling velocity,
524-
// treat it as fling as user raises up his/her last finger that is
525-
// touching the screen.
526-
if ((Math.abs(vx) >= mMinimumFlingVelocity
527-
|| Math.abs(vy) >= mMinimumFlingVelocity)) {
528-
cancelImageTransformations();
529-
getImageTransformer().fling(translationX, translationY, vx, vy,
530-
width, height, mImageBounds.width(), mImageBounds.height());
531-
break;
520+
if (actionMasked == MotionEvent.ACTION_UP) {
521+
mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
522+
final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
523+
final float vy = mVelocityTracker.getYVelocity(mActivePointerId);
524+
// If one of the velocities is not less than our minimum fling velocity,
525+
// treat it as fling as user raises up his/her last finger that is
526+
// touching the screen.
527+
if ((Math.abs(vx) >= mMinimumFlingVelocity
528+
|| Math.abs(vy) >= mMinimumFlingVelocity)) {
529+
cancelImageTransformations();
530+
getImageTransformer().fling(translationX, translationY, vx, vy,
531+
width, height, mImageBounds.width(), mImageBounds.height());
532+
break;
533+
}
532534
}
533535
// Not else!
534536
// Here regard it as a normal scroll

sliding-drawer-layout/src/main/java/com/liuzhenlin/slidingdrawerlayout/SlidingDrawerLayout.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,8 @@ public boolean onTouchEvent(MotionEvent event) {
19451945
mVelocityTracker = VelocityTracker.obtain();
19461946
mVelocityTracker.addMovement(event);
19471947

1948-
switch (event.getAction() & MotionEvent.ACTION_MASK) {
1948+
final int actionMasked = event.getAction() & MotionEvent.ACTION_MASK;
1949+
switch (actionMasked) {
19491950
case MotionEvent.ACTION_POINTER_DOWN:
19501951
onPointerDown(event);
19511952
break;
@@ -1987,16 +1988,18 @@ public boolean onTouchEvent(MotionEvent event) {
19871988
break;
19881989
}
19891990

1990-
mVelocityTracker.computeCurrentVelocity(1000);
1991-
final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
1992-
if (mShownDrawer == mLeftDrawer && vx >= mMinimumFlingVelocity
1993-
|| mShownDrawer == mRightDrawer && vx <= -mMinimumFlingVelocity) {
1994-
openDrawerInternal(mShownDrawer, true);
1995-
break;
1996-
} else if (mShownDrawer == mLeftDrawer && vx <= -mMinimumFlingVelocity
1997-
|| mShownDrawer == mRightDrawer && vx >= mMinimumFlingVelocity) {
1998-
closeDrawer(true);
1999-
break;
1991+
if (actionMasked == MotionEvent.ACTION_UP) {
1992+
mVelocityTracker.computeCurrentVelocity(1000);
1993+
final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
1994+
if (mShownDrawer == mLeftDrawer && vx >= mMinimumFlingVelocity
1995+
|| mShownDrawer == mRightDrawer && vx <= -mMinimumFlingVelocity) {
1996+
openDrawerInternal(mShownDrawer, true);
1997+
break;
1998+
} else if (mShownDrawer == mLeftDrawer && vx <= -mMinimumFlingVelocity
1999+
|| mShownDrawer == mRightDrawer && vx >= mMinimumFlingVelocity) {
2000+
closeDrawer(true);
2001+
break;
2002+
}
20002003
}
20012004

20022005
if (mScrollPercent >= 0.5f) {

0 commit comments

Comments
 (0)