Handle CoverView long-presses better.

This treats them the same way as normal presses: they can be activated with up
to a small amount of movement.

Also executes scroll-reset code even when a press is detected. Before small
movements were not being reset.
This commit is contained in:
Christopher Eby 2011-08-27 15:37:04 -05:00
parent 0303cfb08f
commit 09cf5a8930

View File

@ -220,26 +220,25 @@ public final class CoverView extends View implements Handler.Callback {
{ {
if (mVelocityTracker == null) if (mVelocityTracker == null)
mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(ev); mVelocityTracker.addMovement(ev);
mHandler.removeMessages(MSG_LONG_CLICK); float x = ev.getX();
float y = ev.getY();
int scrollX = getScrollX();
int width = getWidth();
float x = ev.getX(); switch (ev.getAction()) {
int scrollX = getScrollX(); case MotionEvent.ACTION_DOWN:
int width = getWidth(); if (!mScroller.isFinished())
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished())
mScroller.abortAnimation(); mScroller.abortAnimation();
mStartX = x; mStartX = x;
mStartY = ev.getY(); mStartY = y;
mLastMotionX = x; mLastMotionX = x;
mHandler.sendEmptyMessageDelayed(MSG_LONG_CLICK, ViewConfiguration.getLongPressTimeout()); mHandler.sendEmptyMessageDelayed(MSG_LONG_CLICK, ViewConfiguration.getLongPressTimeout());
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE: {
int deltaX = (int) (mLastMotionX - x); int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x; mLastMotionX = x;
@ -252,46 +251,52 @@ public final class CoverView extends View implements Handler.Callback {
if (availableToScroll > 0) if (availableToScroll > 0)
scrollBy(Math.min(availableToScroll, deltaX), 0); scrollBy(Math.min(availableToScroll, deltaX), 0);
} }
if (Math.abs(mStartX - x) + Math.abs(mStartY - y) < 10)
mHandler.removeMessages(MSG_LONG_CLICK);
break; break;
case MotionEvent.ACTION_UP: }
if (Math.abs(mStartX - x) + Math.abs(mStartY - ev.getY()) < 10) { case MotionEvent.ACTION_UP: {
mHandler.removeMessages(MSG_LONG_CLICK);
if (Math.abs(mStartX - x) + Math.abs(mStartY - y) < 10) {
// A long press was performed and thus the normal action should // A long press was performed and thus the normal action should
// not be executed. // not be executed.
if (mIgnoreNextUp) if (mIgnoreNextUp)
mIgnoreNextUp = false; mIgnoreNextUp = false;
else else
performClick(); performClick();
} else {
VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(250);
int velocity = (int) velocityTracker.getXVelocity();
int min = mTimelinePos == 0 ? 1 : 0;
int max = 2;
int nearestCover = (scrollX + width / 2) / width;
int whichCover = Math.max(min, Math.min(nearestCover, max));
if (velocity > SNAP_VELOCITY && whichCover != min)
--whichCover;
else if (velocity < -SNAP_VELOCITY && whichCover != max)
++whichCover;
int newX = whichCover * width;
int delta = newX - scrollX;
mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 2);
if (whichCover != 1)
mTentativeCover = whichCover;
postInvalidate();
} }
VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(250);
int velocity = (int) velocityTracker.getXVelocity();
int min = mTimelinePos == 0 ? 1 : 0;
int max = 2;
int nearestCover = (scrollX + width / 2) / width;
int whichCover = Math.max(min, Math.min(nearestCover, max));
if (velocity > SNAP_VELOCITY && whichCover != min)
--whichCover;
else if (velocity < -SNAP_VELOCITY && whichCover != max)
++whichCover;
int newX = whichCover * width;
int delta = newX - scrollX;
mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 2);
if (whichCover != 1)
mTentativeCover = whichCover;
postInvalidate();
if (mVelocityTracker != null) { if (mVelocityTracker != null) {
mVelocityTracker.recycle(); mVelocityTracker.recycle();
mVelocityTracker = null; mVelocityTracker = null;
} }
break; break;
} }
}
return true; return true;
} }