List of usage examples for android.view MotionEvent ACTION_CANCEL
int ACTION_CANCEL
To view the source code for android.view MotionEvent ACTION_CANCEL.
Click Source Link
From source file:com.cyan.widget.refreshlayout.RefreshLayout.java
private boolean footerTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); int pointerIndex; float y;/*from www. j a v a 2s. c o m*/ float yDiff; switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionY = mInitialMotionY = ev.getY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mIsFooterBeingDragged = false; mFooterCurrPercentage = 0; break; case MotionEvent.ACTION_MOVE: pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (pointerIndex < 0) { Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id."); return false; } y = MotionEventCompat.getY(ev, pointerIndex); yDiff = y - mInitialMotionY; if (!mIsFooterBeingDragged && yDiff < -mTouchSlop) { mIsFooterBeingDragged = true; } if (mIsFooterBeingDragged) { setTriggerPercentage(mAccelerateInterpolator.getInterpolation( MathUtils.clamp(-yDiff, 0, mFooterDistanceToTriggerSync) / mFooterDistanceToTriggerSync)); mLastMotionY = y; } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionY = MotionEventCompat.getY(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER && pointerIndex < 0) { if (action == MotionEvent.ACTION_UP) { Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id."); } return false; } try { y = MotionEventCompat.getY(ev, pointerIndex); } catch (Throwable e) { y = 0; } yDiff = y - mInitialMotionY; if (action == MotionEvent.ACTION_UP && -yDiff > mFooterDistanceToTriggerSync) { // User movement passed distance; trigger a refresh startFooterRefresh(); } else { mCancel.run(); } mIsFooterBeingDragged = false; mFooterCurrPercentage = 0; mActivePointerId = INVALID_POINTER; return false; } return mIsFooterBeingDragged; }
From source file:com.datarita.ultimatecamera.turu.views.TwoWayView.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!isEnabled()) { // A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. return isClickable() || isLongClickable(); }/*ww w . j a v a 2 s. c o m*/ if (!mIsAttached) { return false; } boolean needsInvalidate = false; initVelocityTrackerIfNotExists(); mVelocityTracker.addMovement(ev); final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: { if (mDataChanged) { break; } mVelocityTracker.clear(); mScroller.abortAnimation(); final float x = ev.getX(); final float y = ev.getY(); mLastTouchPos = (mIsVertical ? y : x); int motionPosition = pointToPosition((int) x, (int) y); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mTouchRemainderPos = 0; if (mDataChanged) { break; } if (mTouchMode == TOUCH_MODE_FLINGING) { mTouchMode = TOUCH_MODE_DRAGGING; reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL); motionPosition = findMotionRowOrColumn((int) mLastTouchPos); return true; } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) { mTouchMode = TOUCH_MODE_DOWN; triggerCheckForTap(); } mMotionPosition = motionPosition; break; } case MotionEvent.ACTION_MOVE: { final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (index < 0) { Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId + " - did TwoWayView receive an inconsistent " + "event stream?"); return false; } final float pos; if (mIsVertical) { pos = MotionEventCompat.getY(ev, index); } else { pos = MotionEventCompat.getX(ev, index); } if (mDataChanged) { // Re-sync everything if data has been changed // since the scroll operation can query the mAdapter. layoutChildren(); } final float diff = pos - mLastTouchPos + mTouchRemainderPos; final int delta = (int) diff; mTouchRemainderPos = diff - delta; switch (mTouchMode) { case TOUCH_MODE_DOWN: case TOUCH_MODE_TAP: case TOUCH_MODE_DONE_WAITING: // Check if we have moved far enough that it looks more like a // scroll than a tap maybeStartScrolling(delta); break; case TOUCH_MODE_DRAGGING: case TOUCH_MODE_OVERSCROLL: mLastTouchPos = pos; maybeScroll(delta); break; } break; } case MotionEvent.ACTION_CANCEL: cancelCheckForTap(); mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); setPressed(false); View motionView = this.getChildAt(mMotionPosition - mFirstPosition); if (motionView != null) { motionView.setPressed(false); } if (mStartEdge != null && mEndEdge != null) { needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease(); } recycleVelocityTracker(); break; case MotionEvent.ACTION_UP: { switch (mTouchMode) { case TOUCH_MODE_DOWN: case TOUCH_MODE_TAP: case TOUCH_MODE_DONE_WAITING: { final int motionPosition = mMotionPosition; final View child = getChildAt(motionPosition - mFirstPosition); final float x = ev.getX(); final float y = ev.getY(); boolean inList = false; if (mIsVertical) { inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight(); } else { inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom(); } if (child != null && !child.hasFocusable() && inList) { if (mTouchMode != TOUCH_MODE_DOWN) { child.setPressed(false); } if (mPerformClick == null) { mPerformClick = new PerformClick(); } final PerformClick performClick = mPerformClick; performClick.mClickMotionPosition = motionPosition; performClick.rememberWindowAttachCount(); mResurrectToPosition = motionPosition; if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) { if (mTouchMode == TOUCH_MODE_DOWN) { cancelCheckForTap(); } else { cancelCheckForLongPress(); } mLayoutMode = LAYOUT_NORMAL; if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { mTouchMode = TOUCH_MODE_TAP; setPressed(true); positionSelector(mMotionPosition, child); child.setPressed(true); if (mSelector != null) { Drawable d = mSelector.getCurrent(); if (d != null && d instanceof TransitionDrawable) { ((TransitionDrawable) d).resetTransition(); } } if (mTouchModeReset != null) { removeCallbacks(mTouchModeReset); } mTouchModeReset = new Runnable() { @Override public void run() { mTouchMode = TOUCH_MODE_REST; setPressed(false); child.setPressed(false); if (!mDataChanged) { performClick.run(); } mTouchModeReset = null; } }; postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration()); } else { mTouchMode = TOUCH_MODE_REST; updateSelectorState(); } } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { performClick.run(); } } mTouchMode = TOUCH_MODE_REST; updateSelectorState(); break; } case TOUCH_MODE_DRAGGING: if (contentFits()) { mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); break; } mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final float velocity; if (mIsVertical) { velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId); } else { velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId); } if (Math.abs(velocity) >= mFlingVelocity) { mTouchMode = TOUCH_MODE_FLINGING; reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0), (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE), (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0)); mLastTouchPos = 0; needsInvalidate = true; } else { mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } break; case TOUCH_MODE_OVERSCROLL: mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); break; } cancelCheckForTap(); cancelCheckForLongPress(); setPressed(false); if (mStartEdge != null && mEndEdge != null) { needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease(); } recycleVelocityTracker(); break; } } if (needsInvalidate) { ViewCompat.postInvalidateOnAnimation(this); } return true; }
From source file:com.callba.phone.widget.refreshlayout.RefreshLayout.java
private boolean footerTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); int pointerIndex; float y;//w w w .ja va 2 s .c o m float yDiff; switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionY = mInitialMotionY = ev.getY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mIsFooterBeingDragged = false; mFooterCurrPercentage = 0; break; case MotionEvent.ACTION_MOVE: pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (pointerIndex < 0) { Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id."); return false; } y = MotionEventCompat.getY(ev, pointerIndex); yDiff = y - mInitialMotionY; if (!mIsFooterBeingDragged && yDiff < -mTouchSlop) { mIsFooterBeingDragged = true; } if (mIsFooterBeingDragged) { setTriggerPercentage(mAccelerateInterpolator.getInterpolation( MathUtils.clamp(-yDiff, 0, mFooterDistanceToTriggerSync) / mFooterDistanceToTriggerSync)); mLastMotionY = y; } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionY = MotionEventCompat.getY(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER && pointerIndex < 0) { if (action == MotionEvent.ACTION_UP) { Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id."); } return false; } try { y = MotionEventCompat.getY(ev, pointerIndex); } catch (Throwable e) { y = 0; } yDiff = y - mInitialMotionY; if (action == MotionEvent.ACTION_UP && -yDiff > mFooterDistanceToTriggerSync) { // User movement passed distance; trigger a refresh if (mListener != null) startFooterRefresh(); } else { mCancel.run(); } mIsFooterBeingDragged = false; mFooterCurrPercentage = 0; mActivePointerId = INVALID_POINTER; return false; } return mIsFooterBeingDragged; }
From source file:android.support.v7.widget.RecyclerView.java
private boolean dispatchOnItemTouch(MotionEvent e) { final int action = e.getAction(); if (mActiveOnItemTouchListener != null) { if (action == MotionEvent.ACTION_DOWN) { // Stale state from a previous gesture, we're starting a new one. Clear it. mActiveOnItemTouchListener = null; } else {/*from ww w . j a va 2 s . c o m*/ mActiveOnItemTouchListener.onTouchEvent(this, e); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { // Clean up for the next gesture. mActiveOnItemTouchListener = null; } return true; } } // Listeners will have already received the ACTION_DOWN via dispatchOnItemTouchIntercept // as called from onInterceptTouchEvent; skip it. if (action != MotionEvent.ACTION_DOWN) { final int listenerCount = mOnItemTouchListeners.size(); for (int i = 0; i < listenerCount; i++) { final OnItemTouchListener listener = mOnItemTouchListeners.get(i); if (listener.onInterceptTouchEvent(this, e)) { mActiveOnItemTouchListener = listener; return true; } } } return false; }
From source file:com.cliff.hsj.ui.widget.swipeback.ViewDragHelper.java
/** * Process a touch event received by the parent view. This method will * dispatch callback events as needed before returning. The parent view's * onTouchEvent implementation should call this. * * @param ev The touch event received by the parent view *///from w w w . j a va 2 s . com public void processTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); final int actionIndex = MotionEventCompat.getActionIndex(ev); if (action == MotionEvent.ACTION_DOWN) { // Reset things for a new event stream, just in case we didn't get // the whole previous stream. cancel(); } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); final int pointerId = MotionEventCompat.getPointerId(ev, 0); final View toCapture = findTopChildUnder((int) x, (int) y); saveInitialMotion(x, y, pointerId); // Since the parent is already directly processing this touch // event, // there is no reason to delay for a slop before dragging. // Start immediately if possible. tryCaptureViewForDrag(toCapture, pointerId); final int edgesTouched = mInitialEdgeTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } break; } case MotionEvent.ACTION_MOVE: { if (mDragState == STATE_DRAGGING) { final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, index); final float y = MotionEventCompat.getY(ev, index); final int idx = (int) (x - mLastMotionX[mActivePointerId]); final int idy = (int) (y - mLastMotionY[mActivePointerId]); dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy); saveLastMotion(ev); } else if (mDragState == STATE_JUDGING) { // Check to see if any pointer is now over a draggable view. final int pointerCount = MotionEventCompat.getPointerCount(ev); for (int i = 0; i < pointerCount; i++) { final int pointerId = MotionEventCompat.getPointerId(ev, i); final float x = MotionEventCompat.getX(ev, i); final float y = MotionEventCompat.getY(ev, i); final float dx = x - mInitialMotionX[pointerId]; final float dy = y - mInitialMotionY[pointerId]; reportNewEdgeDrags(dx, dy, pointerId); if (mDragState == STATE_DRAGGING) { // Callback might have started an edge drag. break; } final View toCapture = findTopChildUnder((int) x, (int) y); int slop = checkTouchSlop(toCapture, dx, dy); if (slop == -1) cancel(); else if (slop > 0 && tryCaptureViewForDrag(toCapture, pointerId)) { break; } } saveLastMotion(ev); } break; } case MotionEvent.ACTION_UP: { if (mDragState == STATE_DRAGGING) { releaseViewForPointerUp(); } cancel(); break; } case MotionEvent.ACTION_CANCEL: { if (mDragState == STATE_DRAGGING) { dispatchViewReleased(0, 0); } cancel(); break; } } }
From source file:cn.d.fesa.wuf.ui.view.LazyViewPager.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mFakeDragging) { // A fake drag is in progress already, ignore this real one // but still eat the touch events. // (It is likely that the user is multi-touching the screen.) return true; }/* w ww . j av a 2s . c om*/ if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { // Don't handle edge touches immediately -- they may actually belong to one of our // descendants. return false; } if (mAdapter == null || mAdapter.getCount() == 0) { // Nothing to present or scroll; nothing to touch. return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); boolean needsInvalidate = false; switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ completeScroll(); // Remember where the motion event started mLastMotionX = mInitialMotionX = ev.getX(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_MOVE: if (!mIsBeingDragged) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float xDiff = Math.abs(x - mLastMotionX); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastMotionY); if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff); if (xDiff > mTouchSlop && xDiff > yDiff) { if (DEBUG) Log.v(TAG, "Starting drag!"); mIsBeingDragged = true; mLastMotionX = x; setScrollState(SCROLL_STATE_DRAGGING); setScrollingCacheEnabled(true); } } if (mIsBeingDragged) { // Scroll to follow the motion event final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; final int width = getWidth(); final int widthWithMargin = width + mPageMargin; final int lastItemIndex = mAdapter.getCount() - 1; final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin); final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin; if (scrollX < leftBound) { if (leftBound == 0) { float over = -scrollX; needsInvalidate = mLeftEdge.onPull(over / width); } scrollX = leftBound; } else if (scrollX > rightBound) { if (rightBound == lastItemIndex * widthWithMargin) { float over = scrollX - rightBound; needsInvalidate = mRightEdge.onPull(over / width); } scrollX = rightBound; } // Don't lose the rounded component mLastMotionX += scrollX - (int) scrollX; scrollTo((int) scrollX, getScrollY()); if (mOnPageChangeListener != null) { final int position = (int) scrollX / widthWithMargin; final int positionOffsetPixels = (int) scrollX % widthWithMargin; final float positionOffset = (float) positionOffsetPixels / widthWithMargin; mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } } break; case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); mPopulatePending = true; final int widthWithMargin = getWidth() + mPageMargin; final int scrollX = getScrollX(); final int currentPage = scrollX / widthWithMargin; int nextPage = initialVelocity > 0 ? currentPage : currentPage + 1; setCurrentItemInternal(nextPage, true, true, initialVelocity); mActivePointerId = INVALID_POINTER; endDrag(); needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease(); } break; case MotionEvent.ACTION_CANCEL: if (mIsBeingDragged) { setCurrentItemInternal(mCurItem, true, true); mActivePointerId = INVALID_POINTER; endDrag(); needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, index); mLastMotionX = x; mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } if (needsInvalidate) { invalidate(); } return true; }
From source file:com.boutline.sports.helpers.TwoWayView.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!mIsAttached || mAdapter == null) { return false; }//from w w w . j a v a2s.c o m final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: initOrResetVelocityTracker(); mVelocityTracker.addMovement(ev); mScroller.abortAnimation(); final float x = ev.getX(); final float y = ev.getY(); mLastTouchPos = (mIsVertical ? y : x); final int motionPosition = findMotionRowOrColumn((int) mLastTouchPos); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mTouchRemainderPos = 0; if (mTouchMode == TOUCH_MODE_FLINGING) { return true; } else if (motionPosition >= 0) { mMotionPosition = motionPosition; mTouchMode = TOUCH_MODE_DOWN; } break; case MotionEvent.ACTION_MOVE: { if (mTouchMode != TOUCH_MODE_DOWN) { break; } initVelocityTrackerIfNotExists(); mVelocityTracker.addMovement(ev); final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (index < 0) { Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId + " - did TwoWayView receive an inconsistent " + "event stream?"); return false; } final float pos; if (mIsVertical) { pos = MotionEventCompat.getY(ev, index); } else { pos = MotionEventCompat.getX(ev, index); } final float diff = pos - mLastTouchPos + mTouchRemainderPos; final int delta = (int) diff; mTouchRemainderPos = diff - delta; if (maybeStartScrolling(delta)) { return true; } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mActivePointerId = INVALID_POINTER; mTouchMode = TOUCH_MODE_REST; recycleVelocityTracker(); reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); break; } return false; }
From source file:com.aliasapps.seq.scroller.TwoWayView.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!mIsAttached) { return false; }/*from ww w . j a v a2s . c o m*/ final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: initOrResetVelocityTracker(); mVelocityTracker.addMovement(ev); mScroller.abortAnimation(); final float x = ev.getX(); final float y = ev.getY(); mLastTouchPos = (mIsVertical ? y : x); final int motionPosition = findMotionRowOrColumn((int) mLastTouchPos); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mTouchRemainderPos = 0; if (mTouchMode == TOUCH_MODE_FLINGING) { return true; } else if (motionPosition >= 0) { mMotionPosition = motionPosition; mTouchMode = TOUCH_MODE_DOWN; } break; case MotionEvent.ACTION_MOVE: { if (mTouchMode != TOUCH_MODE_DOWN) { break; } initVelocityTrackerIfNotExists(); mVelocityTracker.addMovement(ev); final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (index < 0) { Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId + " - did TwoWayView receive an inconsistent " + "event stream?"); return false; } final float pos; if (mIsVertical) { pos = MotionEventCompat.getY(ev, index); } else { pos = MotionEventCompat.getX(ev, index); } final float diff = pos - mLastTouchPos + mTouchRemainderPos; final int delta = (int) diff; mTouchRemainderPos = diff - delta; if (maybeStartScrolling(delta)) { return true; } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mActivePointerId = INVALID_POINTER; mTouchMode = TOUCH_MODE_REST; recycleVelocityTracker(); reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); break; } return false; }
From source file:com.android.kit.swipeback.ViewDragHelper.java
/** * Process a touch event received by the parent view. This method will * dispatch callback events as needed before returning. The parent view's * onTouchEvent implementation should call this. * * @param ev The touch event received by the parent view */// w w w .j a va2 s. co m public void processTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); final int actionIndex = MotionEventCompat.getActionIndex(ev); if (action == MotionEvent.ACTION_DOWN) { // Reset things for a new event stream, just in case we didn't get // the whole previous stream. cancel(); } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); final int pointerId = MotionEventCompat.getPointerId(ev, 0); saveInitialMotion(x, y, pointerId); final View toCapture = findTopChildUnder((int) x, (int) y); // Catch a settling view if possible. tryCaptureViewForDrag(toCapture, pointerId); final int edgesTouched = mInitialEdgeTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } break; } case MotionEventCompat.ACTION_POINTER_DOWN: { final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); final float x = MotionEventCompat.getX(ev, actionIndex); final float y = MotionEventCompat.getY(ev, actionIndex); saveInitialMotion(x, y, pointerId); break; } case MotionEvent.ACTION_MOVE: { if (mDragState == STATE_JUDGING) { final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, i); final float y = MotionEventCompat.getY(ev, i); final float dx = x - mInitialMotionX[mActivePointerId]; final float dy = y - mInitialMotionY[mActivePointerId]; reportNewEdgeDrags(dx, dy, mActivePointerId); if (mDragState == STATE_DRAGGING) { // Callback might have started an edge drag. break; } final View toCapture = findTopChildUnder((int) x, (int) y); int slop = checkTouchSlop(toCapture, dx, dy); if (slop == -1) cancel(); else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) { break; } saveLastMotion(ev); } if (mDragState == STATE_DRAGGING) { final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); //???????? if (index == -1) break; final float x = MotionEventCompat.getX(ev, index); final float y = MotionEventCompat.getY(ev, index); final int idx = (int) (x - mLastMotionX[mActivePointerId]); final int idy = (int) (y - mLastMotionY[mActivePointerId]); dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy); saveLastMotion(ev); } break; } case MotionEventCompat.ACTION_POINTER_UP: { final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); clearMotionHistory(pointerId); break; } case MotionEvent.ACTION_UP: { if (mDragState == STATE_DRAGGING) { releaseViewForPointerUp(); } cancel(); break; } case MotionEvent.ACTION_CANCEL: { if (mDragState == STATE_DRAGGING) { dispatchViewReleased(0, 0); } cancel(); break; } } }
From source file:com.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mFakeDragging) { // A fake drag is in progress already, ignore this real one // but still eat the touch events. // (It is likely that the user is multi-touching the screen.) return true; }/*from w w w . ja v a2 s . c o m*/ if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { // Don't handle edge touches immediately -- they may actually belong to one of our // descendants. return false; } if (mAdapter == null || mAdapter.getCount() == 0) { // Nothing to present or scroll; nothing to touch. return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); boolean needsInvalidate = false; switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ completeScroll(); // Remember where the motion event started mLastMotionX = mInitialMotionX = ev.getX(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_MOVE: if (!mIsBeingDragged) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float xDiff = Math.abs(x - mLastMotionX); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastMotionY); if (DEBUG) { Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff); } if (xDiff > mTouchSlop && xDiff > yDiff) { if (DEBUG) { Log.v(TAG, "Starting drag!"); } mIsBeingDragged = true; mLastMotionX = x; setScrollState(SCROLL_STATE_DRAGGING); setScrollingCacheEnabled(true); } } if (mIsBeingDragged) { // Scroll to follow the motion event final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; final int width = getWidth(); final int widthWithMargin = width + mPageMargin; final int lastItemIndex = mAdapter.getCount() - 1; final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin); final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin; if (scrollX < leftBound) { if (leftBound == 0) { float over = -scrollX; needsInvalidate = mLeftEdge.onPull(over / width); } scrollX = leftBound; } else if (scrollX > rightBound) { if (rightBound == lastItemIndex * widthWithMargin) { float over = scrollX - rightBound; needsInvalidate = mRightEdge.onPull(over / width); } scrollX = rightBound; } // Don't lose the rounded component mLastMotionX += scrollX - (int) scrollX; scrollTo((int) scrollX, getScrollY()); if (mOnPageChangeListener != null) { final int position = (int) scrollX / widthWithMargin; final int positionOffsetPixels = (int) scrollX % widthWithMargin; final float positionOffset = (float) positionOffsetPixels / widthWithMargin; mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } } break; case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); mPopulatePending = true; final int widthWithMargin = getWidth() + mPageMargin; final int scrollX = getScrollX(); final int currentPage = scrollX / widthWithMargin; int nextPage = initialVelocity > 0 ? currentPage : currentPage + 1; setCurrentItemInternal(nextPage, true, true, initialVelocity); mActivePointerId = INVALID_POINTER; endDrag(); needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease(); } break; case MotionEvent.ACTION_CANCEL: if (mIsBeingDragged) { setCurrentItemInternal(mCurItem, true, true); mActivePointerId = INVALID_POINTER; endDrag(); needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, index); mLastMotionX = x; mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } if (needsInvalidate) { invalidate(); } return true; }