List of usage examples for android.view VelocityTracker obtain
static public VelocityTracker obtain()
From source file:ngoctdn.vng.gesture.CupcakeGestureDetector.java
public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: { mVelocityTracker = VelocityTracker.obtain(); if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } else {//from ww w.j ava2s . c o m Log.i(LOG_TAG, "Velocity tracker is null"); } mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); mFirstRawTouchY = ev.getRawY(); mLastRawTouchY = ev.getRawY(); mIsDragging = false; mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_POINTER_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; case MotionEvent.ACTION_MOVE: { final float x = getActiveX(ev); final float y = getActiveY(ev); final float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { // Use Pythagoras to see if drag length is larger than // touch slop mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { if (ev.getPointerCount() == 1) mListener.onDrag(mLastRawTouchY, ev.getRawY(), dx, dy); mLastTouchX = x; mLastTouchY = y; mLastRawTouchY = ev.getRawY(); // if (mListener != null) { // mListener.onScrolling(mLastRawTouchY, ev.getRawY()); // mLastRawTouchY = ev.getRawY(); // } if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } } if (mActivePointerId == -1 && mListener != null) { mListener.onRelease(mFirstRawTouchY, ev.getRawY(), 0); } break; } case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: { if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); // if (mListener != null) { // mListener.onScrolling(mFirstTouchY, mLastTouchY); // } // Compute velocity within the last 1000ms mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity(); // If the velocity is greater than minVelocity, call // listener if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) { mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY); } } } if (mListener != null) { mLastRawTouchY = ev.getRawY(); if (mVelocityTracker != null) mListener.onRelease(mFirstRawTouchY, ev.getRawY(), mVelocityTracker.getYVelocity()); else mListener.onRelease(mFirstRawTouchY, ev.getRawY(), 0); } // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; } } return true; }
From source file:ngoctdn.vng.gesture.DragGestureDetector.java
public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: { mVelocityTracker = VelocityTracker.obtain(); if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } else {//ww w.j a v a 2 s. com Log.i(LOG_TAG, "Velocity tracker is null"); } mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); mFirstRawTouchY = ev.getRawY(); mLastRawTouchY = ev.getRawY(); mIsDragging = false; mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_POINTER_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; case MotionEvent.ACTION_MOVE: { final float x = getActiveX(ev); final float y = getActiveY(ev); final float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { // Use Pythagoras to see if drag length is larger than // touch slop mIsDragging = (float) Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { if (ev.getPointerCount() == 1) mListener.onDrag(mLastRawTouchY, ev.getRawY(), dx, dy); mLastTouchX = x; mLastTouchY = y; mLastRawTouchY = ev.getRawY(); // if (mListener != null) { // mListener.onScrolling(mLastRawTouchY, ev.getRawY()); // mLastRawTouchY = ev.getRawY(); // } if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } } if (mActivePointerId == -1 && mListener != null) { mListener.onRelease(mFirstRawTouchY, ev.getRawY(), 0); } break; } // case MotionEvent.ACTION_CANCEL: { // if (mListener != null) { // mLastRawTouchY = ev.getRawY(); // mListener.onRelease(mFirstRawTouchY, ev.getRawY()); // } // // Recycle Velocity Tracker // if (null != mVelocityTracker) { // mVelocityTracker.recycle(); // mVelocityTracker = null; // } // break; // } case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: { if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); // if (mListener != null) { // mListener.onScrolling(mFirstTouchY, mLastTouchY); // } // Compute velocity within the last 1000ms mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity(); // If the velocity is greater than minVelocity, call // listener if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) { mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY); } } } if (mListener != null) { mLastRawTouchY = ev.getRawY(); if (mVelocityTracker != null) mListener.onRelease(mFirstRawTouchY, ev.getRawY(), mVelocityTracker.getYVelocity()); else mListener.onRelease(mFirstRawTouchY, ev.getRawY(), 0); } // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; } } return true; }
From source file:co.adrianblan.fastbrush.MyGLSurfaceView.java
@Override public boolean onTouchEvent(MotionEvent e) { // MotionEvent reports input details from the touch screen // and other input controls. In this case, you are only // interested in events where the touch position changed. switch (e.getAction()) { case MotionEvent.ACTION_DOWN: if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else {/*from ww w . j a v a 2 s.co m*/ mVelocityTracker.clear(); } queueEvent(new Runnable() { @Override public void run() { mRenderer.touchHasStarted(); } }); // No break is intentional case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(e); // Compute velocity in pixels per second mVelocityTracker.computeCurrentVelocity(1); final ArrayList<TouchData> touchDataList = new ArrayList<>(e.getHistorySize() + 1); Vector2 viewportPosition; Vector2 viewportVelocity = new Vector2( VelocityTrackerCompat.getXVelocity(mVelocityTracker, e.getActionIndex()), VelocityTrackerCompat.getYVelocity(mVelocityTracker, e.getActionIndex())); // Add previous touch coordinates for (int i = 0; i < e.getHistorySize(); i++) { viewportPosition = new Vector2(e.getHistoricalX(i), e.getHistoricalY(i)); touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity, e.getHistoricalSize(i), e.getHistoricalPressure(i))); } // Add current touch coordinates viewportPosition = new Vector2(e.getX(), e.getY()); touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity, e.getSize(), e.getPressure())); // Ensure we call switchMode() on the OpenGL thread. // queueEvent() is a method of GLSurfaceView that will do this for us. queueEvent(new Runnable() { @Override public void run() { mRenderer.addTouchData(touchDataList); } }); requestRender(); break; case MotionEvent.ACTION_UP: queueEvent(new Runnable() { @Override public void run() { mRenderer.touchHasEnded(); } }); requestRender(); break; } return true; }
From source file:com.focosee.qingshow.widget.MVerticalViewPager.java
public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }/* w ww . java 2 s . c o m*/ mVelocityTracker.addMovement(event); float currentY = event.getRawY(); switch (event.getAction() & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mLastY = currentY; mMoved = false; break; case MotionEvent.ACTION_MOVE: mMoved = true; break; case MotionEvent.ACTION_UP: if (mMoved) { mVelocityTracker.computeCurrentVelocity(1000); int velocity = (int) mVelocityTracker.getYVelocity(); float moveDistance = currentY - mLastY; int nextPage = mCurrentPage; if (moveDistance < 0 && Math.abs(moveDistance) >= SCROLL_PERCENTAGE * mSinglePageHeight || velocity < 0 && Math.abs(velocity) > MIN_FLING_VELOCITY) { ++nextPage; } else if (moveDistance > 0 && Math.abs(moveDistance) >= SCROLL_PERCENTAGE * mSinglePageHeight || velocity > 0 && Math.abs(velocity) > MIN_FLING_VELOCITY) { --nextPage; } setCurrentItem(nextPage); } break; } return true; }
From source file:com.github.shareme.gwsmaterialuikit.library.ccv.CompactCalendarView.java
public CompactCalendarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); compactCalendarController = new CompactCalendarController(new Paint(), new OverScroller(getContext()), new Rect(), attrs, getContext(), Color.argb(255, 233, 84, 81), Color.argb(255, 64, 64, 64), Color.argb(255, 219, 219, 219), VelocityTracker.obtain()); gestureDetector = new GestureDetectorCompat(getContext(), gestureListener); animationHandler = new AnimationHandler(compactCalendarController, this); }
From source file:com.appsimobile.appsii.SidebarHotspot.java
@Override public boolean onTouchEvent(MotionEvent e) { switch (e.getAction()) { case MotionEvent.ACTION_DOWN: { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(e); // remove the background to make sure it does not overlap // the sidebar mIsDragOpening = true;/*from w ww.ja v a 2 s . c o m*/ setBackgroundResource(0); float x = e.getX(); float y = e.getY(); if (mCallback != null) { mSwipeListener = mCallback.open(this, Gesture.TO_CENTER, (int) x, (int) y); mSwipeInProgress = mSwipeListener != null; mState = STATE_AWAITING_RELEASE; if (mVibrate) { vibrate(); } return true; } return false; } case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(e); float x = e.getX(); float y = e.getY(); return detectSwipe(x, y, e); case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: cancelMotionHandling(e, false); return false; } return super.onTouchEvent(e); }
From source file:com.tjych.swip.vertical_t.VerticalViewPager.java
public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }/*from ww w . j a va 2s . c o m*/ mVelocityTracker.addMovement(event); float currentY = event.getRawY(); float currentX = event.getRawX(); switch (event.getAction() & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mLastY = currentY; mLastX = currentX; mMoved = false; return false; case MotionEvent.ACTION_MOVE: float x = event.getRawX() - mLastX; float y = event.getRawY() - mLastY; System.out.println(x + " : " + y); if (Math.abs(x) > Math.abs(y)) { // getParent().requestDisallowInterceptTouchEvent(true); return false; } else { mMoved = true; return true; } case MotionEvent.ACTION_UP: x = event.getRawX() - mLastX; y = event.getRawY() - mLastY; System.out.println(x + " : " + y); if (Math.abs(x) > Math.abs(y)) { // getParent().requestDisallowInterceptTouchEvent(true); return false; } if (mMoved) { mVelocityTracker.computeCurrentVelocity(1000); int velocity = (int) mVelocityTracker.getYVelocity(); float moveDistance = currentY - mLastY; int nextPage = mCurrentPage; if (moveDistance < 0 && Math.abs(moveDistance) >= SCROLL_PERCENTAGE * mSinglePageHeight || velocity < 0 && Math.abs(velocity) > MIN_FLING_VELOCITY) { ++nextPage; } else if (moveDistance > 0 && Math.abs(moveDistance) >= SCROLL_PERCENTAGE * mSinglePageHeight || velocity > 0 && Math.abs(velocity) > MIN_FLING_VELOCITY) { --nextPage; } setCurrentItem(nextPage); } return true; } return true; }
From source file:com.itude.mobile.mobbl.blueprint.app.view.listeners.SwipeDismissRecyclerViewTouchListener.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mIsVertical ? mRecyclerView.getHeight() : mRecyclerView.getWidth(); }// ww w .j a v a2 s .c om switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // TODO: ensure this is a finger, and set a flag // 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; mDownView = mRecyclerView.findChildViewUnder(x, y); 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 if (mIsVertical) { mDownView.animate().translationY(0).alpha(1).setDuration(mAnimationTime).setListener(null); } else { mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = RecyclerView.NO_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (!mSwiping && mDownView != null && mItemTouchCallback != null) { mItemTouchCallback.onTouch(mRecyclerView.getChildPosition(mDownView)); mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; return true; } if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float velocityY = mVelocityTracker.getYVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (mIsVertical) { if (Math.abs(deltaY) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaY > 0; } else if (mMinFlingVelocity <= absVelocityY && absVelocityY <= mMaxFlingVelocity && absVelocityX < absVelocityY && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityY < 0) == (deltaY < 0); dismissRight = mVelocityTracker.getYVelocity() > 0; } if (dismiss && mDownPosition != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; mDownView.animate().translationY(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel mDownView.animate().translationY(0).alpha(1).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; } else { 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 != ListView.INVALID_POSITION) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; mDownView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel mDownView.animate().translationX(0).alpha(1).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 (mIsVertical) { if (Math.abs(deltaY) > mSlop && Math.abs(deltaX) < Math.abs(deltaY) / 2) { mSwiping = true; mSwipingSlop = (deltaY > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { mDownView.setTranslationY(deltaY - mSwipingSlop); mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaY) / mViewWidth))); return true; } } else { 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 | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { mDownView.setTranslationX(deltaX - mSwipingSlop); mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } } break; } } return false; }
From source file:cn.goodjobs.common.view.photodraweeview.ScaleDragDetector.java
private void onTouchDragEvent(int action, MotionEvent ev) { switch (action) { case MotionEvent.ACTION_DOWN: { mVelocityTracker = VelocityTracker.obtain(); if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); }//from www . j a va 2 s. c o m mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); mIsDragging = false; break; } case MotionEvent.ACTION_MOVE: { final float x = getActiveX(ev); final float y = getActiveY(ev); final float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { mScaleDragGestureListener.onDrag(dx, dy); mLastTouchX = x; mLastTouchY = y; if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } } break; } case MotionEvent.ACTION_CANCEL: { if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; } case MotionEvent.ACTION_UP: { if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity(); if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) { mScaleDragGestureListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY); } } } if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; } } }
From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java
public ResolverDrawerLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResolverDrawerLayout, defStyleAttr, 0);/*from ww w . j ava2s . c om*/ mMaxWidth = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_android_maxWidth, -1); mMaxCollapsedHeight = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_maxCollapsedHeight, 0); mMaxCollapsedHeightSmall = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_maxCollapsedHeightSmall, mMaxCollapsedHeight); a.recycle(); mScroller = new OverScroller(context, AnimationUtils.loadInterpolator(context, android.R.interpolator.decelerate_quint)); mVelocityTracker = VelocityTracker.obtain(); final ViewConfiguration vc = ViewConfiguration.get(context); mTouchSlop = vc.getScaledTouchSlop(); mMinFlingVelocity = vc.getScaledMinimumFlingVelocity(); }