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:cl.monsoon.s1next.widget.PhotoView.java
@Override public boolean onTouchEvent(@NonNull MotionEvent event) { if (scaleGestureDetector == null || mGestureDetector == null) { // We're being destroyed; ignore any touch events return true; }//from ww w . j a v a 2s. com scaleGestureDetector.onTouchEvent(event); mGestureDetector.onTouchEvent(event); final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (!mTranslateRunnable.mRunning) { snap(); } break; } return true; }
From source file:com.conduit.plastic.widget.NestedWebView.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: { this.mIsBeingDragged = false; this.mActivePointerId = MotionEventCompat.getPointerId(ev, 0); this.startNestedScroll(2); break; }/* ww w. j a v a2 s .co m*/ case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { this.endTouch(); break; } } super.onTouchEvent(ev); return true; } final int actionMasked = MotionEventCompat.getActionMasked(ev); initVelocityTrackerIfNotExists(); MotionEvent vtev = MotionEvent.obtain(ev); final int index = MotionEventCompat.getActionIndex(ev); if (actionMasked == MotionEvent.ACTION_DOWN) { mNestedYOffset = 0; } vtev.offsetLocation(0, mNestedYOffset); this.consumedY = 0; this.direction = 0; boolean onTouchEvent = false; switch (actionMasked) { case MotionEvent.ACTION_DOWN: { // Remember where the motion event started onTouchEvent = super.onTouchEvent(ev); mLastMotionY = (int) (ev.getY() + 0.5f); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); this.preY = vtev.getY(); this.mIsBeingDragged = false; startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); break; } case MotionEventCompat.ACTION_POINTER_DOWN: { onTouchEvent = super.onTouchEvent(ev); mLastMotionY = (int) (MotionEventCompat.getY(ev, index) + 0.5f); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEvent.ACTION_MOVE: final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (activePointerIndex == -1) { Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); break; } if (!mIsBeingDragged && Math.abs(vtev.getY() - this.preY) > mTouchSlop) { final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } mIsBeingDragged = true; } // if(!mIsBeingDragged){ // setLongClickEnable(true); // } final int y = (int) (MotionEventCompat.getY(ev, activePointerIndex) + 0.5f); Log.i(TAG, "mLastMotionY=====" + mLastMotionY); Log.i(TAG, "YYYYYYY=====" + y); int deltaY = mLastMotionY - y; if (deltaY != 0) { this.direction = this.directionDetector.getDirection(deltaY, true, this.scrollStateChangedListener); } if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) { deltaY -= mScrollConsumed[1]; vtev.offsetLocation(0, mScrollOffset[1]); mNestedYOffset += mScrollOffset[1]; } if (mIsBeingDragged) { // setJavaScriptEnable(true); // Scroll to follow the motion event mLastMotionY = y - mScrollOffset[1]; Log.i(TAG, "deltaY===" + deltaY); Log.i(TAG, "this.consumedY===" + this.consumedY); final int unconsumedY = deltaY - this.consumedY; Log.i(TAG, " child consumed = " + this.mScrollConsumed[1] + " un_consumed = " + unconsumedY + " position = " + this.position + " direction = " + this.direction); onTouchEvent = super.onTouchEvent(ev); if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) { return true; } switch (this.direction) { case 1: { if ((this.position != ScrollStateChangedListener.ScrollState.BOTTOM) && (this.contentHeight != this.webviewHeight)) { scrollBy(0, unconsumedY); break; } Log.i(TAG, "1111111consumedY===" + consumedY + " unconsumedY==" + unconsumedY); if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) { vtev.offsetLocation(0.0F, this.mScrollOffset[1]); this.mNestedYOffset += this.mScrollOffset[1]; this.mLastMotionY -= this.mScrollOffset[1]; } } break; case 2: if ((this.position == ScrollStateChangedListener.ScrollState.TOP) || (this.contentHeight == this.webviewHeight)) { Log.i(TAG, "2222222consumedY===" + consumedY + " unconsumedY==" + unconsumedY); if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) { vtev.offsetLocation(0.0F, this.mScrollOffset[1]); this.mNestedYOffset += this.mScrollOffset[1]; this.mLastMotionY -= this.mScrollOffset[1]; } } else { scrollBy(0, unconsumedY); } break; default: break; } } break; case MotionEvent.ACTION_CANCEL: onTouchEvent = super.onTouchEvent(ev); break; case MotionEvent.ACTION_UP: onTouchEvent = super.onTouchEvent(ev); if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId); if ((Math.abs(initialVelocity) > mMinimumVelocity)) { flingWithNestedDispatch(-initialVelocity); } } mActivePointerId = INVALID_POINTER; endTouch(); break; case MotionEventCompat.ACTION_POINTER_UP: onTouchEvent = super.onTouchEvent(ev); onSecondaryPointerUp(ev); mLastMotionY = (int) (MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)) + 0.5F); break; } if (mVelocityTracker != null) { mVelocityTracker.addMovement(vtev); } vtev.recycle(); return onTouchEvent; }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardView.java
@Override public boolean onTouchEvent(@NonNull MotionEvent me) { if (getKeyboard() == null)//I mean, if there isn't any keyboard I'm handling, what's the point? return false; if (areTouchesDisabled(me)) { return super.onTouchEvent(me); }/*from w ww.j a v a2s . c o m*/ final int action = MotionEventCompat.getActionMasked(me); // Gesture detector must be enabled only when mini-keyboard is not // on the screen. if (!mMiniKeyboardPopup.isShowing() && mGestureDetector != null && mGestureDetector.onTouchEvent(me)) { Logger.d(TAG, "Gesture detected!"); mKeyPressTimingHandler.cancelAllMessages(); dismissAllKeyPreviews(); return true; } if (action == MotionEvent.ACTION_DOWN) { mFirstTouchPoint.x = (int) me.getX(); mFirstTouchPoint.y = (int) me.getY(); mIsFirstDownEventInsideSpaceBar = mSpaceBarKey != null && mSpaceBarKey.isInside(mFirstTouchPoint.x, mFirstTouchPoint.y); } // If the motion event is above the keyboard and it's a MOVE event // coming even before the first MOVE event into the extension area if (!mIsFirstDownEventInsideSpaceBar && me.getY() < mExtensionKeyboardYActivationPoint && !mMiniKeyboardPopup.isShowing() && !mExtensionVisible && action == MotionEvent.ACTION_MOVE) { if (mExtensionKeyboardAreaEntranceTime <= 0) mExtensionKeyboardAreaEntranceTime = SystemClock.uptimeMillis(); if (SystemClock.uptimeMillis() - mExtensionKeyboardAreaEntranceTime > DELAY_BEFORE_POPPING_UP_EXTENSION_KBD) { KeyboardExtension extKbd = ((ExternalAnyKeyboard) getKeyboard()).getExtensionLayout(); if (extKbd == null || extKbd.getKeyboardResId() == AddOn.INVALID_RES_ID) { Logger.i(TAG, "No extension keyboard"); return super.onTouchEvent(me); } else { // telling the main keyboard that the last touch was // canceled MotionEvent cancel = MotionEvent.obtain(me.getDownTime(), me.getEventTime(), MotionEvent.ACTION_CANCEL, me.getX(), me.getY(), 0); super.onTouchEvent(cancel); cancel.recycle(); mExtensionVisible = true; dismissAllKeyPreviews(); if (mExtensionKey == null) { mExtensionKey = new AnyKey(new Row(getKeyboard()), getThemedKeyboardDimens()); mExtensionKey.edgeFlags = 0; mExtensionKey.height = 1; mExtensionKey.width = 1; mExtensionKey.popupResId = extKbd.getKeyboardResId(); mExtensionKey.externalResourcePopupLayout = mExtensionKey.popupResId != 0; mExtensionKey.x = getWidth() / 2; mExtensionKey.y = mExtensionKeyboardPopupOffset; } // so the popup will be right above your finger. mExtensionKey.x = (int) me.getX(); onLongPress(extKbd, mExtensionKey, AnyApplication.getConfig().isStickyExtensionKeyboard(), getPointerTracker(me)); // it is an extension.. getMiniKeyboard().setPreviewEnabled(true); return true; } } else { return super.onTouchEvent(me); } } else if (mExtensionVisible && me.getY() > mExtensionKeyboardYDismissPoint) { // closing the popup dismissPopupKeyboard(); return true; } else { return super.onTouchEvent(me); } }
From source file:com.appeaser.sublimepickerlibrary.datepicker.DayPickerViewPager.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mCanPickRange) { return super.onTouchEvent(ev); }// w w w . jav a2 s. c o m // looks like the ViewPager wants to step in if (mCheckForLongPress != null) { removeCallbacks(mCheckForLongPress); } if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) { if (Config.DEBUG) { Log.i(TAG, "OTE: LONGPRESS && (UP || CANCEL)"); } if (ev.getAction() == MotionEvent.ACTION_UP) { if (mDayPickerPagerAdapter != null) { mTempSelectedDate = mDayPickerPagerAdapter.resolveEndDateForRange((int) ev.getX(), (int) ev.getY(), getCurrentItem(), false); mDayPickerPagerAdapter.onDateRangeSelectionEnded(mTempSelectedDate); } } mIsLongPressed = false; mInitialDownX = -1; mInitialDownY = -1; mScrollingDirection = NOT_SCROLLING; if (mScrollerRunnable != null) { removeCallbacks(mScrollerRunnable); } //return true; } else if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_DOWN) { if (Config.DEBUG) { Log.i(TAG, "OTE: LONGPRESS && DOWN"); } mScrollingDirection = NOT_SCROLLING; } else if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_MOVE) { if (Config.DEBUG) { Log.i(TAG, "OTE: LONGPRESS && MOVE"); } int direction = resolveDirectionForScroll(ev.getX()); boolean directionChanged = mScrollingDirection != direction; if (directionChanged) { if (mScrollerRunnable != null) { removeCallbacks(mScrollerRunnable); } } if (mScrollerRunnable == null) { mScrollerRunnable = new ScrollerRunnable(); } mScrollingDirection = direction; if (mScrollingDirection == NOT_SCROLLING) { if (mDayPickerPagerAdapter != null) { mTempSelectedDate = mDayPickerPagerAdapter.resolveEndDateForRange((int) ev.getX(), (int) ev.getY(), getCurrentItem(), true); if (mTempSelectedDate != null) { mDayPickerPagerAdapter.onDateRangeSelectionUpdated(mTempSelectedDate); } } } else if (directionChanged) { // SCROLLING_LEFT || SCROLLING_RIGHT post(mScrollerRunnable); } } return mIsLongPressed || super.onTouchEvent(ev); }
From source file:com.android.nobug.view.viewpager.indicator.CirclePageIndicator.java
public boolean onTouchEvent(MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }/* w ww .j av a 2 s. c o m*/ if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { mLastMotionX = x; if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) { mViewPager.fakeDragBy(deltaX); } } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage - 1); } return true; } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage + 1); } return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag(); break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return true; }
From source file:adapters.CirclePageIndicator.java
public boolean onTouchEvent(MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }/* w w w . ja va 2 s. c o m*/ if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { mLastMotionX = x; if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) { mViewPager.fakeDragBy(deltaX); } } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage - 1); } return true; } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage + 1); } return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag(); break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return true; }
From source file:com.base.app.widget.indicatorview.CirclePageIndicator.java
public boolean onTouchEvent(android.view.MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }// ww w .j av a 2 s. c o m if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { mLastMotionX = x; if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) { mViewPager.fakeDragBy(deltaX); } } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage - 1); } return true; } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage + 1); } return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag(); break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return false; }
From source file:com.benefit.buy.library.viewpagerindicator.CirclePageIndicator.java
@Override public boolean onTouchEvent(android.view.MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }//from w ww . j av a 2 s .co m if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { mLastMotionX = x; if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) { mViewPager.fakeDragBy(deltaX); } } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < (halfWidth - sixthWidth))) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage - 1); } return true; } else if ((mCurrentPage < (count - 1)) && (ev.getX() > (halfWidth + sixthWidth))) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage + 1); } return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) { mViewPager.endFakeDrag(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return true; }
From source file:com.alibaba.akita.widget.CirclePageIndicator.java
public boolean onTouchEvent(android.view.MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }/*from w w w.j av a2 s .c om*/ if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction(); switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { if (!mViewPager.isFakeDragging()) { mViewPager.beginFakeDrag(); } mLastMotionX = x; mViewPager.fakeDragBy(deltaX); } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) { mViewPager.setCurrentItem(mCurrentPage - 1); return true; } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) { mViewPager.setCurrentItem(mCurrentPage + 1); return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag(); 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: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return true; }
From source file:com.asvpdemo.AutoScrollViewPagerCirclePageIndicator.java
public boolean onTouchEvent(MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; }//from ww w. j ava 2 s. c o m if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mLastMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = x - mLastMotionX; if (!mIsDragging) { if (Math.abs(deltaX) > mTouchSlop) { mIsDragging = true; } } if (mIsDragging) { mLastMotionX = x; if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) { mViewPager.fakeDragBy(deltaX); } } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (!mIsDragging) { final int count = mViewPager.getAdapter().getCount(); final int width = getWidth(); final float halfWidth = width / 2f; final float sixthWidth = width / 6f; if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage - 1); } return true; } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) { mViewPager.setCurrentItem(mCurrentPage + 1); } return true; } } mIsDragging = false; mActivePointerId = INVALID_POINTER; if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag(); break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId)); break; } return true; }