List of usage examples for android.view VelocityTracker obtain
static public VelocityTracker obtain()
From source file:com.slidinglayer.SlidingPanel.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHereX(ev, mInitialX) && !allowSlidingFromHereY(ev, mInitialY)) { return false; }//from w w w . j av a 2 s . com final int action = ev.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_OUTSIDE) { mLastTouchAllowed = false; } else { mLastTouchAllowed = true; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: completeScroll(); // Remember where the motion event started mLastX = mInitialX = ev.getX(); mLastY = mInitialY = ev.getY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; case MotionEvent.ACTION_MOVE: if (!mIsDragging) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (pointerIndex == -1) { mActivePointerId = INVALID_POINTER; break; } final float x = MotionEventCompat.getX(ev, pointerIndex); final float xDiff = Math.abs(x - mLastX); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastY); if (xDiff > mTouchSlop && xDiff > yDiff) { mIsDragging = true; mLastX = x; setDrawingCacheEnabled(true); } else if (yDiff > mTouchSlop && yDiff > xDiff) { mIsDragging = true; mLastY = y; setDrawingCacheEnabled(true); } } if (mIsDragging) { // Scroll to follow the motion event final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (activePointerIndex == -1) { mActivePointerId = INVALID_POINTER; break; } final float x = MotionEventCompat.getX(ev, activePointerIndex); final float y = MotionEventCompat.getY(ev, activePointerIndex); final float deltaX = mLastX - x; final float deltaY = mLastY - y; mLastX = x; mLastY = y; final float oldScrollX = getScrollX(); final float oldScrollY = getScrollY(); float scrollX = oldScrollX + deltaX; float scrollY = oldScrollY + deltaY; // Log.d("Layer", String.format("Layer scrollX[%f],scrollY[%f]", scrollX, scrollY)); final float leftBound, rightBound; final float bottomBound, topBound; topBound = getHeight(); bottomBound = rightBound = leftBound = 0; if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } if (scrollY > topBound) { scrollY = topBound; } else if (scrollY < bottomBound) { scrollY = bottomBound; } // Keep the precision mLastX += scrollX - (int) scrollX; mLastY += scrollY - (int) scrollY; scrollTo((int) scrollX, (int) scrollY); } break; case MotionEvent.ACTION_UP: if (mIsDragging) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int initialVelocityX = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); final int initialVelocityY = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId); final int scrollX = getScrollX(); final int scrollY = getScrollY(); final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float y = MotionEventCompat.getY(ev, activePointerIndex); final int totalDeltaX = (int) (x - mInitialX); final int totalDeltaY = (int) (y - mInitialY); boolean nextStateOpened = determineNextStateOpened(mIsOpen, scrollX, scrollY, initialVelocityX, initialVelocityY, totalDeltaX, totalDeltaY); switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY); mActivePointerId = INVALID_POINTER; endDrag(); } else if (mIsOpen && closeOnTapEnabled) { closeLayer(true); } else if (!mIsOpen && openOnTapEnabled) { openLayer(true); } break; case MotionEvent.ACTION_CANCEL: if (mIsDragging) { switchLayer(mIsOpen, true, true); mActivePointerId = INVALID_POINTER; endDrag(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastX = MotionEventCompat.getX(ev, index); mLastY = MotionEventCompat.getY(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); mLastX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); mLastY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } if (mActivePointerId == INVALID_POINTER) { mLastTouchAllowed = false; } return true; }
From source file:com.jp.fristandroidapp.widget.adview.AdViewFlow.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (getChildCount() == 0) return false; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }//w w w .jav a 2s. co m mVelocityTracker.addMovement(ev); final int action = ev.getAction(); final float x = ev.getX(); switch (action) { case MotionEvent.ACTION_DOWN: /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mLastMotionX = x; mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; if (handler != null) handler.removeMessages(0); break; case MotionEvent.ACTION_MOVE: final int xDiff = (int) Math.abs(x - mLastMotionX); boolean xMoved = xDiff > mTouchSlop; if (xMoved) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; } if (mTouchState == TOUCH_STATE_SCROLLING) { // Scroll to follow the motion event final int deltaX = (int) (mLastMotionX - x); mLastMotionX = x; final int scrollX = getScrollX(); if (deltaX < 0) { if (scrollX > 0) { scrollBy(Math.max(-scrollX, deltaX), 0); } } else if (deltaX > 0) { final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth(); if (availableToScroll > 0) { scrollBy(Math.min(availableToScroll, deltaX), 0); } } return true; } break; case MotionEvent.ACTION_UP: if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int velocityX = (int) velocityTracker.getXVelocity(); if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) { // Fling hard enough to move left snapToScreen(mCurrentScreen - 1); } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) { // Fling hard enough to move right snapToScreen(mCurrentScreen + 1); } // else if (velocityX < -SNAP_VELOCITY // && mCurrentScreen == getChildCount() - 1) { // snapToScreen(0); // } // else if (velocityX > SNAP_VELOCITY // && mCurrentScreen == 0) { // snapToScreen(getChildCount() - 1); // } else { snapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } } mTouchState = TOUCH_STATE_REST; if (handler != null) { Message message = handler.obtainMessage(0); handler.sendMessageDelayed(message, timeSpan); } break; case MotionEvent.ACTION_CANCEL: snapToDestination(); mTouchState = TOUCH_STATE_REST; if (handler != null) { Message message = handler.obtainMessage(0); handler.sendMessageDelayed(message, timeSpan); } } return true; }
From source file:com.coco.slidinguppanel.SlidingUpPanel.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { // This method JUST determines whether we want to intercept the motion. // If we return true, onMotionEvent will be called and we do the actual // scrolling there. final int action = MotionEventCompat.getActionMasked(ev); // Always take care of the touch gesture being complete. if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { // Release the drag. DEBUG_LOG("Intercept done!"); endDrag();//from w w w . ja v a 2s. co m return false; } // Nothing more to do here if we have decided whether or not we are dragging. if (action != MotionEvent.ACTION_DOWN) { if (mIsBeingDragged) { DEBUG_LOG("Intercept returning true!"); return true; } if (mIsUnableToDrag) { DEBUG_LOG("Intercept returning false!"); return false; } } // Check whether the user has moved far enough from his original down touch. switch (action) { case MotionEvent.ACTION_DOWN: { onTouchDown(ev, true); DEBUG_LOG("***Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged + " mIsUnableToDrag=" + mIsUnableToDrag); break; } case MotionEvent.ACTION_MOVE: { final int activePointerId = mActivePointerId; if (activePointerId == INVALID_POINTER) { // If we don't have a valid id, the touch down wasn't on content. break; } final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); final float xDiff = Math.abs(x - mInitialMotionX); final float yDiff = Math.abs(y - mInitialMotionY); DEBUG_LOG("***Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff); onTouchMove(x, y, xDiff, yDiff, true); break; } case MotionEvent.ACTION_POINTER_UP: onTouchPointerUp(ev); break; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); // The only time we want to intercept motion events is if we are in the drag mode. return mIsBeingDragged; }
From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.swipedismiss.SwipeTouchListener.java
private boolean handleDownEvent(@Nullable final View view, @NonNull final MotionEvent motionEvent) { if (!mSwipeEnabled) { return false; }/* w w w . java2 s .c o m*/ // ? Row View downView = findDownView(motionEvent); if (downView == null) { return false; } int downPosition = AdapterViewUtil.getPositionForView(mListViewWrapper, downView); mCanDismissCurrent = isDismissable(downPosition); /* Check if we are processing the item at this position */ if (mCurrentPosition == downPosition || downPosition >= mVirtualListCount) { return false; } if (view != null) { view.onTouchEvent(motionEvent); } disableHorizontalScrollContainerIfNecessary(motionEvent, downView); mDownX = motionEvent.getX(); mDownY = motionEvent.getY(); mCurrentView = downView; // ? ? ? ( ? ?) mSwipingView = getSwipeView(downView); // ? ? !! mLeftSwipingView = getLeftSwipeView(downView); // ? ? !! mRightSwipingView = getRightSwipeView(downView); // ? ? !! mCurrentPosition = downPosition; mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); return true; }
From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java
/** * Called by the framework when your view for creating input needs to * be generated. This will be called the first time your input method * is displayed, and every time it needs to be re-created such as due to * a configuration change.//www. j ava2 s . c o m */ @Override public View onCreateInputView() { mInputView = (KbLatinKeyboardView) getLayoutInflater().inflate(R.layout.input, null); mInputView.setKeyboard(mQwertyKeyboard); mInputView.setOnKeyboardActionListener(this); /****************************************************edit 2**************************************************************/ featuredb = new DataDBHelper(this); KbSoftKeyboard gd = new KbSoftKeyboard(); sf = new Save_feature(); swipeWord = new GetSwipeWord(this); //gDetect.setOnDoubleTapListener(gd); mInputView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int index = event.getActionIndex(); int action = event.getActionMasked(); int pointerId = event.getPointerId(index); //check for actions of motion event if (event.getAction() == MotionEvent.ACTION_DOWN) { //retrieve key at current char ch = getkeylabel(event); if (ch > 0) swipe += ch; // test+=swipe.charAt(0); //set up start timer for measuring duration // start=System.currentTimeMillis(); //setup velocity tracker if (mvel == null) { // Retrieve a new VelocityTracker object to watch the velocity of a motion. mvel = VelocityTracker.obtain(); } else { // Reset the velocity tracker back to its initial state. mvel.clear(); } } if (event.getAction() == MotionEvent.ACTION_MOVE) { mvel.addMovement(event); mvel.computeCurrentVelocity(1000); // Log velocity of pixels per second // Best practice to use VelocityTrackerCompat where possible. x_vel += abs(VelocityTrackerCompat.getXVelocity(mvel, pointerId)); y_vel += abs(VelocityTrackerCompat.getYVelocity(mvel, pointerId)); n_event += 1; // Log.d("", "X velocity: " + x_vel); // Log.d("", "Y velocity: " + y_vel); } if (event.getAction() == MotionEvent.ACTION_UP) { //record time when finger lifted up // end=System.currentTimeMillis(); //calculate duration // duration=(end-start)/100; //calculate velocity pixels per sec // Log.d("", "X velocity: " + x_vel); // Log.d("", "Y velocity: " + y_vel); velocity = Math.sqrt(x_vel * x_vel + y_vel * y_vel); //obtain pressure pressure += event.getPressure(); np_event += 1; swipe2 = get_final_string(swipe); swipe = ""; if (swipe2 == null) swipe2 = ""; //print generated string System.out.println(swipe + "\n 2nd " + swipe2); } if (((int) old_x) == 0 & ((int) old_y) == 0) { old_x = event.getX(); old_y = event.getY(); swipe += getkeylabel(event); } else if (((int) olddir_x) == 0 & ((int) olddir_y) == 0) { olddir_x = event.getX() - old_x; olddir_y = event.getY() - old_y; old_x = event.getX(); old_y = event.getY(); } else { check_change(event); } // Return false to avoid consuming the touch event return false; } }); //***********************************************edit 2 end***************************************************************** //Saved my life if (mInputView.isPreviewEnabled()) mInputView.setPreviewEnabled(false); return mInputView; }
From source file:com.kaplandroid.colorbook.SlidingLayer.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!mEnabled) { return false; }//from w w w . j ava2 s . c om final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mIsDragging = false; mIsUnableToDrag = false; mActivePointerId = INVALID_POINTER; if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } return false; } if (action != MotionEvent.ACTION_DOWN) { if (mIsDragging) { return true; } else if (mIsUnableToDrag) { return false; } } switch (action) { case MotionEvent.ACTION_MOVE: final int activePointerId = mActivePointerId; if (activePointerId == INVALID_POINTER) { break; } final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId); if (pointerIndex == -1) { mActivePointerId = INVALID_POINTER; break; } final float x = MotionEventCompat.getX(ev, pointerIndex); final float dx = x - mLastX; final float xDiff = Math.abs(dx); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastY); if (xDiff > mTouchSlop && xDiff > yDiff && allowDraging(dx)) { mIsDragging = true; mLastX = x; setDrawingCacheEnabled(true); } else if (yDiff > mTouchSlop) { mIsUnableToDrag = true; } break; case MotionEvent.ACTION_DOWN: mActivePointerId = ev.getAction() & (Build.VERSION.SDK_INT >= 8 ? MotionEvent.ACTION_POINTER_INDEX_MASK : MotionEvent.ACTION_POINTER_INDEX_MASK); mLastX = mInitialX = MotionEventCompat.getX(ev, mActivePointerId); mLastY = MotionEventCompat.getY(ev, mActivePointerId); if (allowSlidingFromHere(ev)) { mIsDragging = false; mIsUnableToDrag = false; // If nobody else got the focus we use it to close the layer return super.onInterceptTouchEvent(ev); } else { mIsUnableToDrag = true; } break; case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; } if (!mIsDragging) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); } return mIsDragging; }
From source file:org.telegram.ui.Components.NumberPicker.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; }//from w w w .j a va 2s .c o m if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_MOVE: { if (mIngonreMoveEvents) { break; } float currentMoveY = event.getY(); if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY); if (deltaDownY > mTouchSlop) { removeAllCallbacks(); onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL); } } else { int deltaMoveY = (int) ((currentMoveY - mLastDownOrMoveEventY)); scrollBy(0, deltaMoveY); invalidate(); } mLastDownOrMoveEventY = currentMoveY; } break; case MotionEvent.ACTION_UP: { removeChangeCurrentByOneFromLongPress(); mPressedStateHelper.cancel(); VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); int initialVelocity = (int) velocityTracker.getYVelocity(); if (Math.abs(initialVelocity) > mMinimumFlingVelocity) { fling(initialVelocity); onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); } else { int eventY = (int) event.getY(); int deltaMoveY = (int) Math.abs(eventY - mLastDownEventY); long deltaTime = event.getEventTime() - mLastDownEventTime; if (deltaMoveY <= mTouchSlop && deltaTime < ViewConfiguration.getTapTimeout()) { int selectorIndexOffset = (eventY / mSelectorElementHeight) - SELECTOR_MIDDLE_ITEM_INDEX; if (selectorIndexOffset > 0) { changeValueByOne(true); mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_INCREMENT); } else if (selectorIndexOffset < 0) { changeValueByOne(false); mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_DECREMENT); } } else { ensureScrollWheelAdjusted(); } onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } mVelocityTracker.recycle(); mVelocityTracker = null; } break; } return true; }
From source file:com.coco.slidinguppanel.SlidingUpPanel.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (getState() == STATE_OPENED) { // disable touch handle when in opened state. return false; }/* w ww . ja va 2 s.c om*/ final int action = MotionEventCompat.getActionMasked(ev); if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { // Don't handle edge touches immediately -- they may actually belong to one of our descendants. return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action) { case MotionEvent.ACTION_DOWN: { onTouchDown(ev, false); DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged + " mIsUnableToDrag=" + mIsUnableToDrag); break; } case MotionEvent.ACTION_MOVE: { final int activePointerId = mActivePointerId; if (activePointerId == INVALID_POINTER || mIsUnableToDrag) { // If we don't have a valid id, the touch down wasn't on content. break; } final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); final float xDiff = Math.abs(x - mInitialMotionX); final float yDiff = Math.abs(y - mInitialMotionY); DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff); onTouchMove(x, y, xDiff, yDiff, false); break; } case MotionEvent.ACTION_UP: { if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId); final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float y = MotionEventCompat.getY(ev, pointerIndex); final int totalDelta = (int) (y - mInitialMotionY); boolean toOpen = determineToOpen(initialVelocity, totalDelta); startFling(toOpen, initialVelocity); endDrag(); } DEBUG_LOG("Touch up!!!"); break; } case MotionEvent.ACTION_CANCEL: { if (mIsBeingDragged) { startFling(isOpen(), 0); endDrag(); } DEBUG_LOG("Touch cancel!!!"); break; } case MotionEventCompat.ACTION_POINTER_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); mLastMotionX = x; mLastMotionY = y; mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex); break; } case MotionEvent.ACTION_POINTER_UP: onTouchPointerUp(ev); break; } return true; }
From source file:com.kaplandroid.colorbook.SlidingLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHere(ev)) { return false; }/*from w w w .j ava 2 s .c o m*/ final int action = ev.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_OUTSIDE) { mLastTouchAllowed = false; } else { mLastTouchAllowed = true; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: completeScroll(); // Remember where the motion event started mLastX = mInitialX = ev.getX(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; case MotionEvent.ACTION_MOVE: if (!mIsDragging) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (pointerIndex == -1) { mActivePointerId = INVALID_POINTER; break; } final float x = MotionEventCompat.getX(ev, pointerIndex); final float xDiff = Math.abs(x - mLastX); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastY); if (xDiff > mTouchSlop && xDiff > yDiff) { mIsDragging = true; mLastX = x; setDrawingCacheEnabled(true); } } if (mIsDragging) { // Scroll to follow the motion event final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (activePointerIndex == -1) { mActivePointerId = INVALID_POINTER; break; } final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = mLastX - x; mLastX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; final float leftBound = mScreenSide < STICK_TO_RIGHT ? getWidth() : 0; final float rightBound = mScreenSide == STICK_TO_LEFT ? 0 : -getWidth(); if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } // Keep the precision mLastX += scrollX - (int) scrollX; scrollTo((int) scrollX, getScrollY()); } break; case MotionEvent.ACTION_UP: if (mIsDragging) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); final int scrollX = getScrollX(); final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final int totalDelta = (int) (x - mInitialX); boolean nextStateOpened = determineNextStateOpened(mIsOpen, scrollX, initialVelocity, totalDelta); switchLayer(nextStateOpened, true, true, initialVelocity); mActivePointerId = INVALID_POINTER; endDrag(); } else if (mIsOpen && closeOnTapEnabled) { closeLayer(true); } break; case MotionEvent.ACTION_CANCEL: if (mIsDragging) { switchLayer(mIsOpen, true, true); mActivePointerId = INVALID_POINTER; endDrag(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, index); mLastX = x; mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); mLastX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } if (mActivePointerId == INVALID_POINTER) { mLastTouchAllowed = false; } return true; }
From source file:com.blestep.sportsbracelet.view.TimelineChartView.java
/** * {@inheritDoc}//w w w . j a v a2s. c o m */ @Override public boolean onTouchEvent(final MotionEvent event) { final int action = event.getActionMasked(); final int index = event.getActionIndex(); final int pointerId = event.getPointerId(index); switch (action) { case MotionEvent.ACTION_DOWN: // Initialize velocity tracker if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } mVelocityTracker.addMovement(event); mScroller.forceFinished(true); mState = STATE_INITIALIZE; mInitialTouchOffset = mCurrentOffset; mInitialTouchX = event.getX(); mInitialTouchY = event.getY(); return true; case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(event); float diffX = event.getX() - mInitialTouchX; float diffY = event.getY() - mInitialTouchY; if (Math.abs(diffX) > mTouchSlop || mState >= STATE_MOVING) { mCurrentOffset = mInitialTouchOffset + diffX; if (mCurrentOffset < 0) { onOverScroll(); mCurrentOffset = 0; } else if (mCurrentOffset > mMaxOffset) { onOverScroll(); mCurrentOffset = mMaxOffset; } mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity); mState = STATE_MOVING; ViewCompat.postInvalidateOnAnimation(this); } else if (Math.abs(diffY) > mTouchSlop && mState < STATE_MOVING) { return false; } return true; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (mState >= STATE_MOVING) { final int velocity = (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId); mScroller.forceFinished(true); mState = STATE_FLINGING; mScroller.fling((int) mCurrentOffset, 0, velocity, 0, 0, (int) mMaxOffset, 0, 0); ViewCompat.postInvalidateOnAnimation(this); } else { // Reset scrolling state mState = STATE_IDLE; } return true; } return false; }