Example usage for android.view MotionEvent getX

List of usage examples for android.view MotionEvent getX

Introduction

In this page you can find the example usage for android.view MotionEvent getX.

Prototype

public final float getX() 

Source Link

Document

#getX(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:com.android.launcher3.BubbleTextView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Call the superclass onTouchEvent first, because sometimes it changes the state to
    // isPressed() on an ACTION_UP
    boolean result = super.onTouchEvent(event);

    // Check for a stylus button press, if it occurs cancel any long press checks.
    if (mStylusEventHelper.onMotionEvent(event)) {
        mLongPressHelper.cancelLongPress();
        result = true;/*  w w w. ja v  a2 s  . c o m*/
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // So that the pressed outline is visible immediately on setStayPressed(),
        // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
        // to create it)
        if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
            mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
        }

        // If we're in a stylus button press, don't check for long press.
        if (!mStylusEventHelper.inStylusButtonPressed()) {
            mLongPressHelper.postCheckForLongPress();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        // If we've touched down and up on an item, and it's still not "pressed", then
        // destroy the pressed outline
        if (!isPressed()) {
            mPressedBackground = null;
        }

        mLongPressHelper.cancelLongPress();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
            mLongPressHelper.cancelLongPress();
        }
        break;
    }
    return result;
}

From source file:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    // "|" used deliberately here; both methods should be invoked.
    final boolean interceptForDrag = mLeftDragger.shouldInterceptTouchEvent(ev)
            | mRightDragger.shouldInterceptTouchEvent(ev);

    boolean interceptForTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;/*from  w w  w  . j a  v a  2s .c o m*/
        mInitialMotionY = y;
        if (mScrimOpacity > 0) {
            final View child = mLeftDragger.findTopChildUnder((int) x, (int) y);
            if (child != null && isContentView(child)) {
                interceptForTap = true;
            }
        }
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // If we cross the touch slop, don't perform the delayed peek for an edge touch.
        if (mLeftDragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) {
            mLeftCallback.removeCallbacks();
            mRightCallback.removeCallbacks();
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        closeDrawers(true);
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
    }
    }

    return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
}

From source file:android.improving.utils.views.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.
 * //  w ww.  j  a v a  2  s . com
 * @param ev
 *            The touch event received by the parent view
 */
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 = mInitialEdgesTouched[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);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal
            // down event.

            final View toCapture = findTopChildUnder((int) x, (int) y);
            tryCaptureViewForDrag(toCapture, pointerId);

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is
            // under this
            // point, we'll swap to controlling it with this pointer
            // instead.
            // (This will still work if we're "catching" a settling
            // view.)

            tryCaptureViewForDrag(mCapturedView, 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 {
            // 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);
                if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to
            // the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int id = MotionEventCompat.getPointerId(ev, i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the
                // view, release it.
                releaseViewForPointerUp();
            }
        }
        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:keyboard.ecloga.com.eclogakeyboard.EclogaKeyboard.java

@Override
public boolean onTouch(View v, final MotionEvent event) {
    mGestureDetector.onTouchEvent(event);

    if (event.getPointerCount() > 1) {
        return true;
    }/*from  w w w . java  2 s. co m*/

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = event.getX();
        mDownY = event.getY();
        isOnClick = true;
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (isOnClick) {
            swipe = false;
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (isOnClick && (Math.abs(mDownX - event.getX()) > 120 || Math.abs(mDownY - event.getY()) > 120)) {
            swipe = true;
            isOnClick = false;
        }
        break;
    default:
        break;
    }

    return false;
}

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;
    }/*from w w w.  ja  va2 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:cn.org.eshow.framwork.view.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();
    }//w w  w .j  a v a2  s .  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:android.support.v7.widget.helper.ItemTouchHelper.java

private View findChildView(MotionEvent event) {
    // first check elevated views, if none, then call RV
    final float x = event.getX();
    final float y = event.getY();
    if (mSelected != null) {
        final View selectedView = mSelected.itemView;
        if (hitTest(selectedView, x, y, mSelectedStartX + mDx, mSelectedStartY + mDy)) {
            return selectedView;
        }//from  w  w w.ja v  a  2s . c o  m
    }
    for (int i = mRecoverAnimations.size() - 1; i >= 0; i--) {
        final RecoverAnimation anim = mRecoverAnimations.get(i);
        final View view = anim.mViewHolder.itemView;
        if (hitTest(view, x, y, anim.mX, anim.mY)) {
            return view;
        }
    }
    return mRecyclerView.findChildViewUnder(x, y);
}

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;
    }//  w  w w.  j a v  a  2  s. c o  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:cn.bingoogolapple.swipebacklayout.BGASwipeBackLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    // Preserve the open state based on the last view that was touched.
    if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) {
        // After the first things will be slideable.
        final View secondChild = getChildAt(1);
        if (secondChild != null) {
            mPreservedOpenState = !mDragHelper.isViewUnder(secondChild, (int) ev.getX(), (int) ev.getY());
        }//from   w w  w  . j  a v a2s .c o  m
    }

    // ========================  START ========================
    if (!isSwipeBackEnable()) {
        mDragHelper.cancel();
        return super.onInterceptTouchEvent(ev);
    }
    // ========================  END ========================

    if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;

        if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) && isDimmed(mSlideableView)) {
            interceptTap = true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float x = ev.getX();
        final float y = ev.getY();
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int slop = mDragHelper.getTouchSlop();
        if (adx > slop && ady > adx) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
    }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}

From source file:com.dycody.android.idealnote.DetailFragment.java

@SuppressLint("NewApi")
@Override/*  ww  w . j  ava 2 s .  com*/
public boolean onTouch(View v, MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
        Log.v(Constants.TAG, "MotionEvent.ACTION_DOWN");
        int w;

        Point displaySize = Display.getUsableSize(mainActivity);
        w = displaySize.x;

        if (x < Constants.SWIPE_MARGIN || x > w - Constants.SWIPE_MARGIN) {
            swiping = true;
            startSwipeX = x;
        }

        break;

    case MotionEvent.ACTION_UP:
        Log.v(Constants.TAG, "MotionEvent.ACTION_UP");
        if (swiping)
            swiping = false;
        break;

    case MotionEvent.ACTION_MOVE:
        if (swiping) {
            Log.v(Constants.TAG, "MotionEvent.ACTION_MOVE at position " + x + ", " + y);
            if (Math.abs(x - startSwipeX) > Constants.SWIPE_OFFSET) {
                swiping = false;
                FragmentTransaction transaction = mainActivity.getSupportFragmentManager().beginTransaction();
                mainActivity.animateTransition(transaction, mainActivity.TRANSITION_VERTICAL);
                DetailFragment mDetailFragment = new DetailFragment();
                Bundle b = new Bundle();
                b.putParcelable(Constants.INTENT_NOTE, new Note());
                mDetailFragment.setArguments(b);
                transaction.replace(R.id.fragment_container, mDetailFragment, mainActivity.FRAGMENT_DETAIL_TAG)
                        .addToBackStack(mainActivity.FRAGMENT_DETAIL_TAG).commit();
            }
        }
        break;

    default:
        Log.e(Constants.TAG, "Wrong element choosen: " + event.getAction());
    }

    return true;
}