List of usage examples for android.view VelocityTracker computeCurrentVelocity
public void computeCurrentVelocity(int units, float maxVelocity)
From source file:com.glm.view.SlidingLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHereX(ev, mInitialX) && !allowSlidingFromHereY(ev, mInitialY)) { return false; }/*from w w w . j a v 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; switch (mScreenSide) { case STICK_TO_LEFT: topBound = bottomBound = rightBound = 0; leftBound = getWidth(); // How far left we can scroll break; case STICK_TO_MIDDLE: topBound = getHeight(); bottomBound = -getHeight(); leftBound = getWidth(); rightBound = -getWidth(); break; case STICK_TO_RIGHT: rightBound = -getWidth(); topBound = bottomBound = leftBound = 0; break; case STICK_TO_TOP: topBound = getHeight(); bottomBound = rightBound = leftBound = 0; break; case STICK_TO_BOTTOM: topBound = rightBound = leftBound = 0; bottomBound = -getHeight(); break; default: topBound = bottomBound = rightBound = leftBound = 0; break; } 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.bofsoft.sdk.widget.menu.slidingmenu.CustomViewAbove.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mEnabled) return false; if (!mIsBeingDragged && !thisTouchAllowed(ev)) return false; // if (!mIsBeingDragged && !mQuickReturn) // return false; final int action = ev.getAction(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }/*from www . j a v a 2s .c om*/ mVelocityTracker.addMovement(ev); 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 int index = MotionEventCompat.getActionIndex(ev); mActivePointerId = MotionEventCompat.getPointerId(ev, index); mLastMotionX = mInitialMotionX = ev.getX(); break; case MotionEvent.ACTION_MOVE: if (!mIsBeingDragged) { determineDrag(ev); if (mIsUnableToDrag) return false; } if (mIsBeingDragged) { // Scroll to follow the motion event final int activePointerIndex = getPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER) break; final float x = MotionEventCompat.getX(ev, activePointerIndex); final float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; final float leftBound = getLeftBound(); final float rightBound = getRightBound(); if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { scrollX = rightBound; } // Don't lose the rounded component mLastMotionX += scrollX - (int) scrollX; scrollTo((int) scrollX, getScrollY()); pageScrolled((int) scrollX); } break; case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); final int scrollX = getScrollX(); // final int widthWithMargin = getWidth(); // final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin; // TODO test this. should get better flinging behavior final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth(); final int activePointerIndex = getPointerIndex(ev, mActivePointerId); if (mActivePointerId != INVALID_POINTER) { final float x = MotionEventCompat.getX(ev, activePointerIndex); final int totalDelta = (int) (x - mInitialMotionX); int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta); setCurrentItemInternal(nextPage, true, true, initialVelocity); } else { setCurrentItemInternal(mCurItem, true, true, initialVelocity); } mActivePointerId = INVALID_POINTER; endDrag(); } else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) { // close the menu setCurrentItem(1); endDrag(); } break; case MotionEvent.ACTION_CANCEL: if (mIsBeingDragged) { setCurrentItemInternal(mCurItem, true, true); mActivePointerId = INVALID_POINTER; endDrag(); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { final int indexx = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, indexx); mActivePointerId = MotionEventCompat.getPointerId(ev, indexx); break; } case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); int pointerIndex = getPointerIndex(ev, mActivePointerId); if (mActivePointerId == INVALID_POINTER) break; mLastMotionX = MotionEventCompat.getX(ev, pointerIndex); break; } return true; }
From source file:com.android.hcframe.DraggableGridViewPager.java
@Override public boolean onTouchEvent(MotionEvent ev) { 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; }// w ww . ja v a 2 s. c o m if (mPageCount <= 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: { mScroller.abortAnimation(); // Remember where the motion event started mLastMotionX = mInitialMotionX = ev.getX(); mLastMotionY = mInitialMotionY = ev.getY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); HcLog.D("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged + " mIsUnableToDrag=" + mIsUnableToDrag); if (!mIsBeingDragged && mScrollState == SCROLL_STATE_IDLE) { mLastPosition = getPositionByXY((int) mLastMotionX, (int) mLastMotionY); } else { mLastPosition = -1; } if (mLastPosition >= 0) { mLastDownTime = System.currentTimeMillis(); } else { mLastDownTime = Long.MAX_VALUE; } HcLog.D("Down at mLastPosition=" + mLastPosition); mLastDragged = -1; break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); if (mLastDragged >= 0) { // change draw location of dragged visual final View v = getChildAt(mLastDragged); final int l = getScrollX() + (int) x - v.getWidth() / 2; final int t = getScrollY() + (int) y - v.getHeight() / 2; v.layout(l, t, l + v.getWidth(), t + v.getHeight()); // check for new target hover if (mScrollState == SCROLL_STATE_IDLE) { final int target = getTargetByXY((int) x, (int) y); if (target != -1 && mLastTarget != target) { animateGap(target); mLastTarget = target; HcLog.D("Moved to mLastTarget=" + mLastTarget); } // edge holding final int edge = getEdgeByXY((int) x, (int) y); if (mLastEdge == -1) { if (edge != mLastEdge) { mLastEdge = edge; mLastEdgeTime = System.currentTimeMillis(); } } else { if (edge != mLastEdge) { mLastEdge = -1; } else { if ((System.currentTimeMillis() - mLastEdgeTime) >= EDGE_HOLD_DURATION) { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); triggerSwipe(edge); mLastEdge = -1; } } } } } else if (!mIsBeingDragged) { final float xDiff = Math.abs(x - mLastMotionX); final float yDiff = Math.abs(y - mLastMotionY); HcLog.D("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff); if (xDiff > mTouchSlop && xDiff > yDiff) { HcLog.D("Starting drag!"); mIsBeingDragged = true; requestParentDisallowInterceptTouchEvent(true); mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop; mLastMotionY = y; setScrollState(SCROLL_STATE_DRAGGING); setScrollingCacheEnabled(true); } } // Not else! Note that mIsBeingDragged can be set above. if (mIsBeingDragged) { // Scroll to follow the motion event needsInvalidate |= performDrag(x); } else if (mLastPosition >= 0) { final int currentPosition = getPositionByXY((int) x, (int) y); HcLog.D("Moved to currentPosition=" + currentPosition); if (currentPosition == mLastPosition) { if ((System.currentTimeMillis() - mLastDownTime) >= LONG_CLICK_DURATION) { if (onItemLongClick(currentPosition)) { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); mLastDragged = mLastPosition; requestParentDisallowInterceptTouchEvent(true); mLastTarget = -1; animateDragged(); mLastPosition = -1; } mLastDownTime = Long.MAX_VALUE; } } else { mLastPosition = -1; } } break; } case MotionEvent.ACTION_UP: { HcLog.D("Touch up!!!"); final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); if (mLastDragged >= 0) { rearrange(); } else if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); final int width = getWidth(); final int scrollX = getScrollX(); final int currentPage = scrollX / width; final int offsetPixels = scrollX - currentPage * width; final float pageOffset = (float) offsetPixels / (float) width; final int totalDelta = (int) (x - mInitialMotionX); int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta); setCurrentItemInternal(nextPage, true, true, initialVelocity); mActivePointerId = INVALID_POINTER; endDrag(); } else if (mLastPosition >= 0) { final int currentPosition = getPositionByXY((int) x, (int) y); HcLog.D("Touch up!!! currentPosition=" + currentPosition); if (currentPosition == mLastPosition) { onItemClick(currentPosition); } } break; } case MotionEvent.ACTION_CANCEL: HcLog.D("Touch cancel!!!"); if (mLastDragged >= 0) { rearrange(); } else if (mIsBeingDragged) { scrollToItem(mCurItem, true, 0, false); mActivePointerId = INVALID_POINTER; endDrag(); } 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) { ViewCompat.postInvalidateOnAnimation(this); } return true; }
From source file:bk.vinhdo.taxiads.utils.view.SlidingLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHereX(ev, mInitialX) && !allowSlidingFromHereY(ev, mInitialY)) { return false; }/*ww w .j av a2s . co m*/ 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); if (mOnInteractListener != null) { mOnInteractListener.onActionMove(x, y); } 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; switch (mScreenSide) { case STICK_TO_LEFT: topBound = bottomBound = rightBound = 0; leftBound = getWidth(); // How far left we can scroll break; case STICK_TO_MIDDLE: topBound = getHeight(); bottomBound = -getHeight(); leftBound = getWidth(); rightBound = -getWidth(); break; case STICK_TO_RIGHT: rightBound = -getWidth(); topBound = bottomBound = leftBound = 0; break; case STICK_TO_TOP: topBound = getHeight(); bottomBound = rightBound = leftBound = 0; break; case STICK_TO_BOTTOM: topBound = rightBound = leftBound = 0; bottomBound = -getHeight(); break; default: topBound = bottomBound = rightBound = leftBound = 0; break; } if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } if (scrollY > topBound) { scrollY = topBound; } else if (scrollY < bottomBound) { scrollY = bottomBound; } // TODO top/bottom bounds // 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); boolean isControlled = false; if (isControlled) { break; } switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY); mActivePointerId = INVALID_POINTER; Log.d("", "end drag from action up"); 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; Log.d(TAG, "end drag from action cancel"); 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.example.lansosdk.util.SlidingLayer.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 2s .co 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(); 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; // switch (mScreenSide) { // case STICK_TO_LEFT: // topBound = bottomBound = rightBound = 0; // leftBound = getWidth(); // How far left we can scroll // break; // case STICK_TO_MIDDLE: // topBound = getHeight(); // bottomBound = -getHeight(); // leftBound = getWidth(); // rightBound = -getWidth(); // break; // case STICK_TO_RIGHT: // rightBound = -getWidth(); // topBound = bottomBound = leftBound = 0; // break; // case STICK_TO_TOP: // topBound = getHeight(); // bottomBound = rightBound = leftBound = 0; // break; // case STICK_TO_BOTTOM: // topBound = rightBound = leftBound = 0; // bottomBound = -getHeight(); // break; // default: // topBound = bottomBound = rightBound = leftBound = 0; // break; // } // 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.digreamon.android.widget.SlidingPaneLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mIsSlidable) { return super.onTouchEvent(ev); }//from ww w. ja v a2 s. c o m if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); boolean needsInvalidate = false; boolean wantTouchEvents = true; switch (action & MotionEventCompat.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mInitialMotionX = x; mInitialMotionY = y; if (isSlideablePaneUnder(x, y)) { mScroller.abortAnimation(); wantTouchEvents = true; mLastMotionX = x; setScrollState(SCROLL_STATE_DRAGGING); } break; } case MotionEvent.ACTION_MOVE: { if (mScrollState != SCROLL_STATE_DRAGGING) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); final float dx = Math.abs(x - mLastMotionX); final float dy = Math.abs(y - mLastMotionY); if (dx > mTouchSlop && dx > dy && isSlideablePaneUnder(x, y)) { mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop; setScrollState(SCROLL_STATE_DRAGGING); } } if (mScrollState == SCROLL_STATE_DRAGGING) { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final float y = MotionEventCompat.getY(ev, activePointerIndex); needsInvalidate |= performDrag(x, y); } break; } case MotionEvent.ACTION_UP: { if (isDimmed(getCoverView())) { final int pi = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pi); final float y = MotionEventCompat.getY(ev, pi); final float dx = x - mInitialMotionX; final float dy = y - mInitialMotionY; if (dx * dx + dy * dy < mTouchSlop * mTouchSlop && isSlideablePaneUnder(x, y)) { // Taps close a dimmed open pane. closePane(getCoverView(), 0); mActivePointerId = INVALID_POINTER; break; } } if (mScrollState == SCROLL_STATE_DRAGGING) { final VelocityTracker vt = mVelocityTracker; vt.computeCurrentVelocity(1000, mMaxVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(vt, mActivePointerId); boolean isPerformClose = false; switch (getViewSpec(mCoveredView)) { case LEFT: isPerformClose = initialVelocity < 0 || (initialVelocity == 0 && mSlideOffset < 0.5f); break; case RIGHT: isPerformClose = initialVelocity > 0 || (initialVelocity == 0 && mSlideOffset < 0.5f); break; default: break; } if (isPerformClose) { closePane(getCoverView(), initialVelocity); } else { openPane(getCoverView(), initialVelocity); } mActivePointerId = INVALID_POINTER; } break; } case MotionEvent.ACTION_CANCEL: { if (mScrollState == SCROLL_STATE_DRAGGING) { mActivePointerId = INVALID_POINTER; if (mSlideOffset < 0.5f) { closePane(getCoverView(), 0); } else { openPane(getCoverView(), 0); } } break; } case MotionEventCompat.ACTION_POINTER_DOWN: { final int index = MotionEventCompat.getActionIndex(ev); mLastMotionX = MotionEventCompat.getX(ev, index); mLastMotionY = MotionEventCompat.getY(ev, index); mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; } case MotionEventCompat.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); break; } } if (needsInvalidate) { invalidate(); } return wantTouchEvents; }
From source file:com.hb.hkm.slidinglayer.SlidLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHere(mInitialX, mInitialY)) { return false; }//from w ww . ja v a 2 s.co 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(); 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 == INVALID_VALUE) { mActivePointerId = INVALID_VALUE; 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); final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff; final boolean validVerticalDrag = yDiff > mTouchSlop && yDiff > xDiff; if (validHorizontalDrag) { mLastX = x; } else if (validVerticalDrag) { mLastY = y; } if (validHorizontalDrag || validVerticalDrag) { mIsDragging = true; setDrawingCacheEnabled(true); } } if (mIsDragging) { // Scroll to follow the motion event final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (activePointerIndex == INVALID_VALUE) { mActivePointerId = INVALID_VALUE; 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; switch (mScreenSide) { case STICK_TO_LEFT: topBound = bottomBound = rightBound = 0; leftBound = getWidth(); // How far left we can scroll break; case STICK_TO_RIGHT: rightBound = -getWidth(); topBound = bottomBound = leftBound = 0; break; case STICK_TO_TOP: topBound = getHeight(); bottomBound = rightBound = leftBound = 0; break; case STICK_TO_BOTTOM: topBound = rightBound = leftBound = 0; bottomBound = -getHeight(); break; default: topBound = bottomBound = rightBound = leftBound = 0; break; } 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; scrollToAndNotify((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); int nextState = determineNextStateForDrag(scrollX, scrollY, initialVelocityX, initialVelocityY, (int) mInitialX, (int) mInitialY, (int) x, (int) y); setLayerState(nextState, true, true, initialVelocityX, initialVelocityY); mActivePointerId = INVALID_VALUE; endDrag(); } else if (changeStateOnTap) { int nextState = determineNextStateAfterTap(); setLayerState(nextState, true, true); } break; case MotionEvent.ACTION_CANCEL: if (mIsDragging) { setLayerState(mCurrentState, true, true); mActivePointerId = INVALID_VALUE; 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_VALUE) { mLastTouchAllowed = false; } return true; }
From source file:com.wunderlist.slidinglayer.SlidingLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { return false; }/*from w ww . j a v a 2s . c om*/ if (!mEnabled || !mIsDragging && !touchPointIsWithinBounds(mInitialX, mInitialY)) { return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { completeScroll(); // Remember where the motion event started mLastX = mInitialRawX = getViewX(ev); mLastY = mInitialRawY = getViewY(ev); mInitialX = ev.getX(); mInitialY = ev.getY(); mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (!touchPointIsWithinBounds(ev.getX(), ev.getY(), false)) return false; final float x = getViewX(ev); final float y = getViewY(ev); final float deltaX = mLastX - x; final float deltaY = mLastY - y; mLastX = x; mLastY = y; if (!mIsDragging) { final float xDiff = Math.abs(x - mInitialRawX); final float yDiff = Math.abs(y - mInitialRawY); final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff; final boolean validVerticalDrag = yDiff > mTouchSlop && yDiff > xDiff; if (validHorizontalDrag || validVerticalDrag) { mIsDragging = true; setDrawingCacheEnabled(true); } } if (mIsDragging) { 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; switch (mScreenSide) { case STICK_TO_LEFT: topBound = bottomBound = rightBound = 0; leftBound = getWidth(); // How far left we can scroll break; case STICK_TO_RIGHT: rightBound = -getWidth(); topBound = bottomBound = leftBound = 0; break; case STICK_TO_TOP: topBound = getHeight(); bottomBound = rightBound = leftBound = 0; break; case STICK_TO_BOTTOM: bottomBound = -getHeight(); topBound = rightBound = leftBound = 0; break; default: topBound = bottomBound = rightBound = leftBound = 0; break; } 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; scrollToAndNotify((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 pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = getViewX(ev); final float y = getViewY(ev); int nextState = determineNextStateForDrag(scrollX, scrollY, initialVelocityX, initialVelocityY, (int) mInitialRawX, (int) mInitialRawY, (int) x, (int) y); setLayerState(nextState, true, true, initialVelocityX, initialVelocityY); mActivePointerId = INVALID_VALUE; endDrag(); } else if (changeStateOnTap) { int nextState = determineNextStateAfterTap(); setLayerState(nextState, true, true); } break; } case MotionEvent.ACTION_CANCEL: if (mIsDragging) { setLayerState(mCurrentState, true, true); mActivePointerId = INVALID_VALUE; endDrag(); } break; case MotionEvent.ACTION_POINTER_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); mActivePointerId = ev.getPointerId(pointerIndex); mLastX = getViewX(ev); mLastY = getViewY(ev); break; } case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); mLastX = getViewX(ev); mLastY = getViewY(ev); break; } } return true; }
From source file:com.yao.zhihudaily.tool.LazyViewPager.java
/** * End a fake drag of the pager./*from w ww. ja v a 2 s . c o m*/ * * @see #beginFakeDrag() * @see #fakeDragBy(float) */ public void endFakeDrag() { if (!mFakeDragging) { throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first."); } final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId); mPopulatePending = true; if ((Math.abs(initialVelocity) > mMinimumVelocity) || Math.abs(mInitialMotionX - mLastMotionX) >= (getWidth() / 3)) { if (mLastMotionX > mInitialMotionX) { setCurrentItemInternal(mCurItem - 1, true, true); } else { setCurrentItemInternal(mCurItem + 1, true, true); } } else { setCurrentItemInternal(mCurItem, true, true); } endDrag(); mFakeDragging = false; }
From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java
@Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); if (!isEnabled()) { if (!mScroller.isFinished()) { mScroller.abortAnimation();/* w ww. ja v a 2 s . c om*/ } snapToScreen(mCurrentScreen); return false; // We don't want the events. Let them fall through to the all // apps view. } acquireVelocityTrackerAndAddMovement(ev); switch (action & MotionEvent.ACTION_MASK) { 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 = ev.getX(); mLastMotionX2 = ev.getX(); mActivePointerId = ev.getPointerId(0); if (mTouchState == TOUCH_STATE_SCROLLING) { enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1); } break; case MotionEvent.ACTION_MOVE: if (mTouchState == TOUCH_STATE_SCROLLING) { // Scroll to follow the motion event final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float deltaX = mLastMotionX - x; final float deltaX2 = mLastMotionX2 - x; final int mode = mOverScrollMode; mLastMotionX = x; if (deltaX < 0) { mTouchX += deltaX; mSmoothingTime = System.nanoTime() / NANOTIME_DIV; if (mTouchX < 0 && mode != OVER_SCROLL_NEVER) { mTouchX = mLastMotionX = 0; // mLastMotionX = x; if (mEdgeGlowLeft != null && deltaX2 < 0) { mEdgeGlowLeft.onPull((float) deltaX / getWidth()); if (!mEdgeGlowRight.isFinished()) { mEdgeGlowRight.onRelease(); } } } invalidate(); } else if (deltaX > 0) { final int totalWidth = getScreenScrollPositionX(mItemCount - 1); final float availableToScroll = getScreenScrollPositionX(mItemCount) - mTouchX; mSmoothingTime = System.nanoTime() / NANOTIME_DIV; mTouchX += Math.min(availableToScroll, deltaX); if (availableToScroll <= getWidth() && mode != OVER_SCROLL_NEVER) { mTouchX = mLastMotionX = totalWidth; // mLastMotionX = x; if (mEdgeGlowLeft != null && deltaX2 > 0) { mEdgeGlowRight.onPull((float) deltaX / getWidth()); if (!mEdgeGlowLeft.isFinished()) { mEdgeGlowLeft.onRelease(); } } } invalidate(); } else { awakenScrollBars(); } } break; case MotionEvent.ACTION_UP: if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId); final int screenWidth = getWidth(); final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth; final float scrolledPos = (float) getScrollX() / screenWidth; if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) { // Fling hard enough to move left. // Don't fling across more than one screen at a time. final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen; snapToScreen(Math.min(whichScreen, bound), velocityX, true); } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < mItemCount - 1) { // Fling hard enough to move right // Don't fling across more than one screen at a time. final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen; snapToScreen(Math.max(whichScreen, bound), velocityX, true); } else { snapToScreen(whichScreen, 0, true); } if (mEdgeGlowLeft != null) { mEdgeGlowLeft.onRelease(); mEdgeGlowRight.onRelease(); } } mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; releaseVelocityTracker(); break; case MotionEvent.ACTION_CANCEL: if (mTouchState == TOUCH_STATE_SCROLLING) { final int screenWidth = getWidth(); final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth; snapToScreen(whichScreen, 0, true); } mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; releaseVelocityTracker(); if (mEdgeGlowLeft != null) { mEdgeGlowLeft.onRelease(); mEdgeGlowRight.onRelease(); } break; case MotionEvent.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; } return true; }