Skip to content

Commit

Permalink
滑动速度追踪的优化
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Apr 24, 2020
1 parent 044d5ea commit df736a3
Showing 1 changed file with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,34 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
mTouching = true;
SCROLL_ORIENTATION = SCROLL_NONE;
mFixedY = ev.getY();
mActivePointerId = ev.getPointerId(actionIndex);
mEventY = (int) ev.getY(actionIndex);
mEventX = (int) ev.getX(actionIndex);

initOrResetAdjustVelocityTracker();
mAdjustVelocityTracker.addMovement(ev);

initOrResetVelocityTracker();
mVelocityTracker.addMovement(ev);
break;
case MotionEvent.ACTION_POINTER_DOWN:
mActivePointerId = ev.getPointerId(actionIndex);
mEventY = (int) ev.getY(actionIndex);
mEventX = (int) ev.getX(actionIndex);
// 改变滑动的手指,重新询问事件拦截
requestDisallowInterceptTouchEvent(false);
initOrResetAdjustVelocityTracker();

initAdjustVelocityTrackerIfNotExists();
mAdjustVelocityTracker.addMovement(ev);

initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
break;
case MotionEvent.ACTION_MOVE:

initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);

final int pointerIndex = ev.findPointerIndex(mActivePointerId);
int offsetY = (int) ev.getY(pointerIndex) - mEventY;
int offsetX = (int) ev.getX(pointerIndex) - mEventX;
Expand Down Expand Up @@ -309,6 +327,9 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
}
initAdjustVelocityTrackerIfNotExists();
mAdjustVelocityTracker.addMovement(ev);

initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Expand All @@ -327,14 +348,27 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
}
}

if (mVelocityTracker != null) {
mVelocityTracker.addMovement(ev);
}

mEventY = 0;
mEventX = 0;
mTouching = false;
SCROLL_ORIENTATION = SCROLL_NONE;
break;
}

return super.dispatchTouchEvent(ev);
boolean dispatch = super.dispatchTouchEvent(ev);

switch (ev.getActionMasked()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
recycleVelocityTracker();
break;
}

return dispatch;
}

@Override
Expand All @@ -356,8 +390,6 @@ public boolean onTouchEvent(MotionEvent ev) {
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
mTouchY = (int) ev.getY(pointerIndex);
initOrResetVelocityTracker();
mVelocityTracker.addMovement(ev);
break;
case MotionEvent.ACTION_MOVE:
if (mTouchY == 0) {
Expand All @@ -370,8 +402,6 @@ public boolean onTouchEvent(MotionEvent ev) {
int oldY = mOwnScrollY;
scrollBy(0, -dy);
int deltaY = -dy;
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);

// 判断是否显示边界阴影
final int range = getScrollRange();
Expand Down Expand Up @@ -409,11 +439,9 @@ public boolean onTouchEvent(MotionEvent ev) {
mTouchY = 0;

if (mVelocityTracker != null) {
mVelocityTracker.addMovement(ev);
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int yVelocity = (int) mVelocityTracker.getYVelocity();
yVelocity = Math.max(-mMaximumVelocity, Math.min(yVelocity, mMaximumVelocity));
recycleVelocityTracker();
fling(-yVelocity);
}
break;
Expand Down

0 comments on commit df736a3

Please sign in to comment.