List of usage examples for android.view VelocityTracker obtain
static public VelocityTracker obtain()
From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java
private boolean down(MotionEvent motionEvent) { if (mPaused)/*from www . j a v a2 s . c o m*/ return false; mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mSwipeView = mRecyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); if (mSwipeView == null) return false; int pos = mRecyclerView.getChildPosition(mSwipeView); mAllowedSwipeDirection = mCallbacks.canDismiss(pos); if (mAllowedSwipeDirection != SwipeDirection.NONE) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); return false; } resetMotion(); return false; }
From source file:com.example.carlitos.swipeitemrecycler.view.animation.swipe_item.swipeable.RecyclerViewSwipeManager.java
/** * Constructor./*w w w . j a va2 s. com*/ */ public RecyclerViewSwipeManager() { mInternalUseOnItemTouchListener = new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return RecyclerViewSwipeManager.this.onInterceptTouchEvent(rv, e); } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { RecyclerViewSwipeManager.this.onTouchEvent(rv, e); } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { RecyclerViewSwipeManager.this.onRequestDisallowInterceptTouchEvent(disallowIntercept); } }; mVelocityTracker = VelocityTracker.obtain(); mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); }
From source file:org.kymjs.kjframe.widget.KJViewPager.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isScroll) { return false; }//from ww w.j a v a 2 s. com if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); final int action = event.getAction(); final float x = event.getX(); final float y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: if (!mScroller.isFinished()) { mScroller.abortAnimation(); } mLastMotionX = x; mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: int deltaX = (int) (mLastMotionX - x); int deltaY = (int) (mLastMotionY - y); if (Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10) break; mLastMotionY = y; mLastMotionX = x; scrollBy(deltaX, 0); break; case MotionEvent.ACTION_UP: // if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); if (velocityX > SNAP_VELOCITY && mCurScreen > 0) { // Fling enough to move left snapToScreen(mCurScreen - 1); } else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) { // Fling enough to move right snapToScreen(mCurScreen + 1); } else { snapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } mTouchState = TOUCH_STATE_REST; break; case MotionEvent.ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; break; } return true; }
From source file:com.arthurpitman.common.SlidingPanel.java
@Override public boolean onTouchEvent(MotionEvent event) { float y = event.getY(); int action = event.getActionMasked(); int pointerId = event.getPointerId(event.getActionIndex()); switch (action) { case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_DOWN: startTouchY = y;//from w w w .j a v a2 s .c om startTouchScrollY = getScrollY(); // if touch didn't occur on the actual control, ignore it float touchBoundary = viewHeight - collapsedHeight - startTouchScrollY; if (y < touchBoundary) { return false; } // start tracking velocity if (velocityTracker == null) { velocityTracker = VelocityTracker.obtain(); } else { velocityTracker.clear(); } velocityTracker.addMovement(event); break; case MotionEvent.ACTION_MOVE: // determine if a valid touch has started if (Math.abs(y - startTouchY) > touchSlop) { touchState = TOUCH_STARTED; } if (velocityTracker != null) { velocityTracker.addMovement(event); } // scroll as appropriate if (touchState == TOUCH_STARTED) { final int scrollDelta = (int) (startTouchY - y); scrollTo(0, Math.max(0, Math.min(viewHeight, startTouchScrollY + scrollDelta))); } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_UP: if (touchState == TOUCH_STARTED) { final int currentScrollY = getScrollY(); // get velocity float velocity = 0; if (velocityTracker != null) { velocityTracker.computeCurrentVelocity(1000); velocity = VelocityTrackerCompat.getYVelocity(velocityTracker, pointerId); velocityTracker.recycle(); velocityTracker = null; } // snap to final scroll position int target = startTouchScrollY; if ((Math.abs(velocity) > minimumflingVelocity) || (Math.abs(y - startTouchY) > (viewHeight / 2))) { if (velocity < 0) { target = viewHeight; } else { target = 0; } } scroller.startScroll(0, currentScrollY, 0, target - currentScrollY); invalidate(); touchState = TOUCH_NONE; } break; } return true; }
From source file:se.kth.csc.stayawhile.swipe.QueueTouchListener.java
private boolean handleTouchEvent(MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }/*from ww w.j a va 2 s . co 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); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } 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) { final int downPosition = mDownPosition; if (dismissRight) { ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime) .setListener(new ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationEnd(View view) { mQueueSwipeListener.onSetHelp(mRecyclerView, new int[] { downPosition }); } }); } else { final View downView = mDownView; // mDownView gets null'd before animation ends ++mDismissAnimationRefCount; mAnimatingPosition = mDownPosition; ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationEnd(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { performDismiss(downView, downPosition); } } }); } } 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 (mSwiping && deltaX < 0) { ViewCompat.setTranslationX(mDownView, deltaX - mSwipingSlop); ViewCompat.setAlpha(mDownView, Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth)))); return true; } else if (mSwiping && deltaX > 0) { ViewCompat.setTranslationX(mDownView, Math.min(deltaX, mViewWidth / 2) - mSwipingSlop); return true; } break; } } return false; }
From source file:com.waz.zclient.pages.main.conversationlist.views.listview.SwipeListView.java
/** * @see android.widget.ListView#onInterceptTouchEvent(android.view.MotionEvent) */// www. j a v a2s.co m @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = MotionEventCompat.getActionMasked(ev); final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: touchState = TOUCH_STATE_REST; velocityTracker = VelocityTracker.obtain(); velocityTracker.addMovement(ev); lastMotionX = x; lastMotionY = y; if (isItemOpen) { getParent().requestDisallowInterceptTouchEvent(true); //return true; } return super.onInterceptTouchEvent(ev); case MotionEvent.ACTION_MOVE: if (touchState != TOUCH_STATE_REST) { return true; } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: velocityTracker.recycle(); velocityTracker = null; break; } return super.onInterceptTouchEvent(ev); }
From source file:cn.org.eshow.framwork.view.sliding.AbSlidingPageView.java
/** * ?./* w w w . j ava 2 s . com*/ * @param event the event */ private void obtainVelocityTracker(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); }
From source file:br.com.devmix.baseapp.listener.OnSwipeableRecyclerViewTouchListener.java
private boolean handleTouchEvent(MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mRecyclerView.getWidth(); }/*from w w w . j a v a2 s .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.getChildAdapterPosition(mDownView)) { mAlpha = ViewCompat.getAlpha(mDownView); mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mRecyclerView.getChildAdapterPosition(mDownView); if (mSwipeListener.canSwipe(mDownPosition)) { 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 ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationEnd(View view) { performDismiss(downView, downPosition); } }); } 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 (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:cn.ieclipse.af.view.ScrollLayout.java
@Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }/* w w w .j a v a 2 s .c o m*/ mVelocityTracker.addMovement(event); final int action = event.getAction(); final float x = event.getX(); final float y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: if (debug) { Log.v(TAG, "event down!"); } if (!mScroller.isFinished()) { mScroller.abortAnimation(); } mLastMotionX = x; break; case MotionEvent.ACTION_MOVE: int deltaX = (int) (mLastMotionX - x); mLastMotionX = x; scrollBy(deltaX, 0); break; case MotionEvent.ACTION_UP: if (debug) { Log.v(TAG, "event : up"); } // if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); if (debug) { Log.v(TAG, "velocityX:" + velocityX); } if (velocityX > mSnapVelocity) { if (mCurScreen > 0) { // Fling enough to move left if (debug) { Log.v(TAG, "snap left"); } snapToScreen(mCurScreen - 1); } else { // TODO disableWipe(false); snapToDestination(); } } else if (velocityX < -mSnapVelocity) { if (mCurScreen < getChildCount() - 1) { // Fling enough to move right if (debug) { Log.v(TAG, "snap right"); } snapToScreen(mCurScreen + 1); } else { // TODO disableWipe(false); snapToDestination(); } } else { snapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } // } mTouchState = TOUCH_STATE_REST; break; case MotionEvent.ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; break; } return true; }
From source file:com.lfq.pulltorefresh.library.sideslip.SwipeMenuLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev);/*from ww w . ja v a 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) { // x view if (mSwipeCurrentHorizontal == null || shouldResetSwipe) { // if (disX < 0) { // if (mSwipeLeftHorizontal != null) // mSwipeCurrentHorizontal = mSwipeLeftHorizontal; // else // mSwipeCurrentHorizontal = mSwipeRightHorizontal; // } else { // if (mSwipeRightHorizontal != null) // mSwipeCurrentHorizontal = mSwipeRightHorizontal; // else // mSwipeCurrentHorizontal = mSwipeLeftHorizontal; // } mSwipeCurrentHorizontal = mSwipeRightHorizontal; } 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); }