List of usage examples for android.view VelocityTracker obtain
static public VelocityTracker obtain()
From source file:com.hks360.library.mtech.swipe.SwipeMenuLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev);//from www. j ava 2 s .c om int dx; int dy; int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: { mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); break; } case MotionEvent.ACTION_MOVE: { if (!isSwipeEnable()) break; int disX = (int) (mLastX - ev.getX()); int disY = (int) (mLastY - ev.getY()); if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) { mDragging = true; } if (mDragging) { if (mSwipeCurrentHorizontal == null || shouldResetSwipe) { if (disX < 0) { if (mSwipeLeftHorizontal != null) mSwipeCurrentHorizontal = mSwipeLeftHorizontal; else mSwipeCurrentHorizontal = mSwipeRightHorizontal; } else { if (mSwipeRightHorizontal != null) mSwipeCurrentHorizontal = mSwipeRightHorizontal; else mSwipeCurrentHorizontal = mSwipeLeftHorizontal; } } scrollBy(disX, 0); mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); shouldResetSwipe = false; } break; } case MotionEvent.ACTION_UP: { dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); mDragging = false; mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity); int velocityX = (int) mVelocityTracker.getXVelocity(); int velocity = Math.abs(velocityX); if (velocity > mScaledMinimumFlingVelocity) { if (mSwipeCurrentHorizontal != null) { int duration = getSwipeDuration(ev, velocity); if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) { if (velocityX < 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } else { if (velocityX > 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } ViewCompat.postInvalidateOnAnimation(this); } } else { judgeOpenClose(dx, dy); } mVelocityTracker.clear(); mVelocityTracker.recycle(); mVelocityTracker = null; if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop || isLeftMenuOpen() || isRightMenuOpen()) { ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; } break; } case MotionEvent.ACTION_CANCEL: { mDragging = false; if (!mScroller.isFinished()) { mScroller.abortAnimation(); } else { dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); judgeOpenClose(dx, dy); } break; } } return super.onTouchEvent(ev); }
From source file:org.zywx.wbpalmstar.engine.EBounceView.java
@Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }/*from w w w . j av a2 s . c o m*/ mVelocityTracker.addMovement(event); int y = (int) event.getRawY(); int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mLastY = y; break; case MotionEvent.ACTION_MOVE: int m = y - mLastY; if (mIsTop) { moveDown(m); } else if (mIsBottom) { moveUp(m); } mLastY = y; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_OUTSIDE: reverse(); handlerTracker(); break; } return true; }
From source file:com.example.administrator.myapplication.recycler_swipe.swpie.SwipeMenuLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev);//w w w .j a va2 s . c o m int dx; int dy; int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: { mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); break; } case MotionEvent.ACTION_MOVE: { if (!isSwipeEnable()) break; int disX = (int) (mLastX - ev.getX()); int disY = (int) (mLastY - ev.getY()); if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) { mDragging = true; } if (mDragging) { if (mSwipeCurrentHorizontal == null || shouldResetSwipe) { if (disX < 0) { if (mSwipeLeftHorizontal != null) mSwipeCurrentHorizontal = mSwipeLeftHorizontal; else mSwipeCurrentHorizontal = mSwipeRightHorizontal; } else { if (mSwipeRightHorizontal != null) mSwipeCurrentHorizontal = mSwipeRightHorizontal; else mSwipeCurrentHorizontal = mSwipeLeftHorizontal; } } scrollBy(disX, 0); mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); shouldResetSwipe = false; } break; } case MotionEvent.ACTION_UP: { dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); mDragging = false; mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity); int velocityX = (int) mVelocityTracker.getXVelocity(); int velocity = Math.abs(velocityX); //?menu if (velocity > mScaledMinimumFlingVelocity) { if (mSwipeCurrentHorizontal != null) { int duration = getSwipeDuration(ev, velocity); if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) { if (velocityX < 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } else { if (velocityX > 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } ViewCompat.postInvalidateOnAnimation(this); } } else { judgeOpenClose(dx, dy); } mVelocityTracker.clear(); mVelocityTracker.recycle(); mVelocityTracker = null; if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop || isLeftMenuOpen() || isRightMenuOpen()) { ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; } break; } case MotionEvent.ACTION_CANCEL: { mDragging = false; if (!mScroller.isFinished()) { mScroller.abortAnimation(); } else { dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); judgeOpenClose(dx, dy); } break; } } return super.onTouchEvent(ev); }
From source file:com.andview.refreshview.swipe.SwipeMenuLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev);/*w w w. j a v a 2 s . c o m*/ int dx; int dy; int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE: if (!isSwipeEnable()) break; int disX = (int) (mLastX - ev.getX()); int disY = (int) (mLastY - ev.getY()); if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) { mDragging = true; } if (mDragging) { if (mSwipeCurrentHorizontal == null || shouldResetSwipe) { if (disX < 0) { if (mSwipeLeftHorizontal != null) mSwipeCurrentHorizontal = mSwipeLeftHorizontal; else mSwipeCurrentHorizontal = mSwipeRightHorizontal; } else { if (mSwipeRightHorizontal != null) mSwipeCurrentHorizontal = mSwipeRightHorizontal; else mSwipeCurrentHorizontal = mSwipeLeftHorizontal; } } scrollBy(disX, 0); mLastX = (int) ev.getX(); mLastY = (int) ev.getY(); shouldResetSwipe = false; } break; case MotionEvent.ACTION_UP: dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); mDragging = false; mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity); int velocityX = (int) mVelocityTracker.getXVelocity(); int velocity = Math.abs(velocityX); if (velocity > mScaledMinimumFlingVelocity) { if (mSwipeCurrentHorizontal != null) { int duration = getSwipeDuration(ev, velocity); if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) { if (velocityX < 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } else { if (velocityX > 0) { smoothOpenMenu(duration); } else { smoothCloseMenu(duration); } } ViewCompat.postInvalidateOnAnimation(this); } } else { judgeOpenClose(dx, dy); } mVelocityTracker.clear(); mVelocityTracker.recycle(); mVelocityTracker = null; if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop || isMenuOpen()) { ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; } break; case MotionEvent.ACTION_CANCEL: mDragging = false; if (!mScroller.isFinished()) { mScroller.abortAnimation(); } else { dx = (int) (mDownX - ev.getX()); dy = (int) (mDownY - ev.getY()); judgeOpenClose(dx, dy); } break; } return super.onTouchEvent(ev); }
From source file:com.aako.zjp2p.widget.superrecycler.swipe.SwipeDismissRecyclerViewTouchListener.java
@SuppressLint("AndroidLintClickableViewAccessibility") @Override//from www . j a v a2 s.com public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); } switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildPosition(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { setTranslationX(mDownView, deltaX - mSwipingSlop); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:com.adamin.superrecyclerview.superrecycer.swipe.SwipeDismissRecyclerViewTouchListener.java
@SuppressLint("AndroidLintClickableViewAccessibility") @Override// ww w . j a v a 2s. com public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); } switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildPosition(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime) .setListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() { @Override public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) { super.onAnimationEnd(animation); performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { setTranslationX(mDownView, deltaX - mSwipingSlop); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:cn.meiqu.baseproject.view.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }/*from ww w . j a v a2 s . c o m*/ switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildPosition(mDownView); if (mCallbacks.canDismiss(mDownPosition)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { setTranslationX(mDownView, deltaX - mSwipingSlop); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:io.nuclei.cyto.ui.view.BottomSheetView.java
@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mVelocityTracker = VelocityTracker.obtain(); }
From source file:com.brandon.mailbox.RecyclerSwipeListener.java
private boolean handleTouchEvent(MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }/* w w w.j a v a 2s . c o m*/ switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { break; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; mRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mRecyclerView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null && mAnimatingPosition != mRecyclerView.getChildLayoutPosition(mDownView)) { mAlpha = ViewCompat.getAlpha(mDownView); mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView); mSwipingLeft = mSwipeListener.canSwipeLeft(mDownPosition); mSwipingRight = mSwipeListener.canSwipeRight(mDownPosition); if (mSwipingLeft || mSwipingRight) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } else { mDownView = null; } } break; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mDownView != null && mSwiping) { // cancel ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } mFinalDelta = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(mFinalDelta) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = mFinalDelta > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (mFinalDelta < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; mAnimatingPosition = mDownPosition; ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListener() { @Override public void onAnimationStart(View view) { // Do nothing. } @Override public void onAnimationEnd(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { performDismiss(downView, downPosition); } } @Override public void onAnimationCancel(View view) { // Do nothing. } }); } else { // cancel ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); } if (deltaX < 0 && !mSwipingLeft) mSwiping = false; if (deltaX > 0 && !mSwipingRight) mSwiping = false; if (mSwiping) { ViewCompat.setTranslationX(mDownView, deltaX - mSwipingSlop); ViewCompat.setAlpha(mDownView, Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth)))); return true; } break; } } return false; }
From source file:com.android2.calculator3.view.CalculatorPadView.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!mInterceptingTouchEvents) { onInterceptTouchEvent(event);//from www .j a v a 2s. c o m return true; } int action = MotionEventCompat.getActionMasked(event); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (action) { case MotionEvent.ACTION_DOWN: // Handled in intercept break; case MotionEvent.ACTION_MOVE: handleMove(event); break; case MotionEvent.ACTION_UP: handleUp(event); break; } return true; }