Example usage for android.view MotionEvent ACTION_DOWN

List of usage examples for android.view MotionEvent ACTION_DOWN

Introduction

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

Prototype

int ACTION_DOWN

To view the source code for android.view MotionEvent ACTION_DOWN.

Click Source Link

Document

Constant for #getActionMasked : A pressed gesture has started, the motion contains the initial starting location.

Usage

From source file:cnedu.ustcjd.widget.MultiSlider.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!mIsUserSeekable || !isEnabled()) {
        return false;
    }/*from  w  ww.j a  v  a  2s. c o  m*/
    final int xx = Math.round(event.getX());
    final int yy = Math.round(event.getY());

    int pointerIdx = event.getActionIndex();

    Thumb currThumb = null;
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN
            || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        LinkedList<Thumb> closestOnes = getClosestThumb((int) event.getX(pointerIdx));

        if (isInScrollingContainer() && mDraggingThumbs.size() == 0 && exactTouched != null && pointerIdx > 0) {
            //we have been here before => we want to use the bar
            Thumb prevThumb = exactTouched.getFirst();
            onStartTrackingTouch(prevThumb);
            exactTouched = null;
        }

        if (closestOnes != null && !closestOnes.isEmpty()) {
            if (closestOnes.size() == 1) {
                currThumb = closestOnes.getFirst();
                if (isInScrollingContainer() && mDraggingThumbs.size() == 0) {
                    exactTouched = closestOnes;
                }
            } else {
                //we have more than one thumb at the same place and we touched there
                exactTouched = closestOnes;
            }
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (exactTouched != null && !exactTouched.isEmpty()) {
            currThumb = getMostMovableThumb(event);
            //check if move actually changed value
            // if (currThumb == null) return false;
        } else if (mDraggingThumbs.size() > pointerIdx) {
            currThumb = mDraggingThumbs.get(pointerIdx);
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_UP
            || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        if (mDraggingThumbs.size() > pointerIdx) {
            currThumb = mDraggingThumbs.get(pointerIdx);
        } //else we had a candidate but was never tracked
        else if (exactTouched != null && exactTouched.size() > 0) {
            currThumb = getMostMovableThumb(event);
            exactTouched = null;
        }
    }
    //        else {
    //            LinkedList<Thumb> closestOnes = getClosestThumb((int) event.getX());
    //            currThumb = closestOnes.getFirst();
    //        }

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        if (isInScrollingContainer() && mDraggingThumbs.size() == 0) {
            mTouchDownX = event.getX(pointerIdx);
        } else {
            onStartTrackingTouch(currThumb);
            setThumbValue(currThumb, getValue(event, currThumb), true);
            setHotspot(xx, yy, currThumb);
        }
        break;
    //with move we dont have pointer action so set them all
    case MotionEvent.ACTION_MOVE:
        if (mDraggingThumbs.contains(currThumb)) {
            //need the index
            for (int i = 0; i < mDraggingThumbs.size(); i++) {
                if (mDraggingThumbs.get(i) != null && mDraggingThumbs.get(i).getThumb() != null) {
                    invalidate(mDraggingThumbs.get(i).getThumb().getBounds());
                }
                setThumbValue(mDraggingThumbs.get(i), getValue(event, i, mDraggingThumbs.get(i)), true);

            }
            setHotspot(xx, yy, currThumb);
        } else {
            final float x = event.getX(pointerIdx);
            if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {
                onStartTrackingTouch(currThumb);
                exactTouched = null;
                setThumbValue(currThumb, getValue(event, currThumb), true);
                setHotspot(xx, yy, currThumb);
            }
        }

        break;

    case MotionEvent.ACTION_UP:
        setPressed(false);
        //there are other pointers left
    case MotionEvent.ACTION_POINTER_UP:
        if (currThumb != null) {
            boolean toUnPress = false;
            if (!isPressed()) {
                setPressed(true);
                toUnPress = true;
            }
            setThumbValue(currThumb, getValue(event, currThumb), true);
            setHotspot(xx, yy, currThumb);
            onStopTrackingTouch(currThumb);
            if (toUnPress) {
                setPressed(false);
            }
        } else {
            //                    currThumb = getClosestThumb(newValue);
            //                    // Touch up when we never crossed the touch slop threshold should
            //                    // be interpreted as a tap-seek to that location.
            //                    onStartTrackingTouch(currThumb);
            //                    setThumbValue(currThumb, newValue, true);
            //                    onStopTrackingTouch(currThumb);
        }
        // ProgressBar doesn't know to repaint the thumb drawable
        // in its inactive state when the touch stops (because the
        // value has not apparently changed)
        invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mDraggingThumbs != null) {
            onStopTrackingTouch();
            setPressed(false);
        }
        invalidate(); // see above explanation
        break;
    }
    return true;
}

From source file:android.support.v7.widget.RecyclerView.java

private boolean dispatchOnItemTouchIntercept(MotionEvent e) {
    final int action = e.getAction();
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_DOWN) {
        mActiveOnItemTouchListener = null;
    }//from www  . j a v a  2s  .  co m

    final int listenerCount = mOnItemTouchListeners.size();
    for (int i = 0; i < listenerCount; i++) {
        final OnItemTouchListener listener = mOnItemTouchListeners.get(i);
        if (listener.onInterceptTouchEvent(this, e) && action != MotionEvent.ACTION_CANCEL) {
            mActiveOnItemTouchListener = listener;
            return true;
        }
    }
    return false;
}

From source file:com.android.dialer.widget.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *//*from  w w w .j  a  v  a  2 s  . co  m*/
public void processTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        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) {
            int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (index < 0) {
                Log.e(TAG,
                        "Pointer index for id " + mActivePointerId + " not found." + " Skipping MotionEvent");
                return;
            }
            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:android.support.v7.widget.RecyclerView.java

private boolean dispatchOnItemTouch(MotionEvent e) {
    final int action = e.getAction();
    if (mActiveOnItemTouchListener != null) {
        if (action == MotionEvent.ACTION_DOWN) {
            // Stale state from a previous gesture, we're starting a new one. Clear it.
            mActiveOnItemTouchListener = null;
        } else {// w  w w .j a  v  a  2s . c om
            mActiveOnItemTouchListener.onTouchEvent(this, e);
            if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
                // Clean up for the next gesture.
                mActiveOnItemTouchListener = null;
            }
            return true;
        }
    }

    // Listeners will have already received the ACTION_DOWN via dispatchOnItemTouchIntercept
    // as called from onInterceptTouchEvent; skip it.
    if (action != MotionEvent.ACTION_DOWN) {
        final int listenerCount = mOnItemTouchListeners.size();
        for (int i = 0; i < listenerCount; i++) {
            final OnItemTouchListener listener = mOnItemTouchListeners.get(i);
            if (listener.onInterceptTouchEvent(this, e)) {
                mActiveOnItemTouchListener = listener;
                return true;
            }
        }
    }
    return false;
}

From source file:com.aidy.bottomdrawerlayout.AllDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    Log.i(TAG, "onTouchEvent()");
    final int action = ev.getAction();
    boolean wantTouchEvents = true;
    try {/*  ww w.  jav  a 2s .  com*/

        mLeftDragger.processTouchEvent(ev);
        mRightDragger.processTouchEvent(ev);
        mTopDragger.processTouchEvent(ev);
        mBottomDragger.processTouchEvent(ev);

        switch (action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            Log.i(TAG, "onTouchEvent() -- ACTION_DOWN");
            final float x = ev.getX();
            final float y = ev.getY();
            mInitialMotionX = x;
            mInitialMotionY = y;
            mDisallowInterceptRequested = false;
            mChildrenCanceledTouch = false;
            break;
        }

        case MotionEvent.ACTION_UP: {
            Log.i(TAG, "onTouchEvent() -- ACTION_UP");
            final float x = ev.getX();
            final float y = ev.getY();
            boolean peekingOnly = true;
            final View touchedView = mLeftDragger.findTopChildUnder((int) x, (int) y);
            if (touchedView != null && isContentView(touchedView)) {
                final float dx = x - mInitialMotionX;
                final float dy = y - mInitialMotionY;
                final int slop = mLeftDragger.getTouchSlop();
                if (dx * dx + dy * dy < slop * slop) {
                    // Taps close a dimmed open drawer but only if it isn't
                    // locked open.
                    final View openDrawer = findOpenDrawer();
                    if (openDrawer != null) {
                        peekingOnly = getDrawerLockMode(openDrawer) == LOCK_MODE_LOCKED_OPEN;
                    }
                }
            }
            closeDrawers(peekingOnly);
            mDisallowInterceptRequested = false;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            Log.i(TAG, "onTouchEvent() -- ACTION_CANCEL");
            closeDrawers(true);
            mDisallowInterceptRequested = false;
            mChildrenCanceledTouch = false;
            break;
        }
        }
    } catch (IllegalArgumentException e) {
        // TODO: handle exception
    }
    boolean result = wantTouchEvents;
    Log.i(TAG, "onTouchEvent() -- result = " + result);
    return result;
}

From source file:com.androzic.MapFragment.java

@SuppressLint("ClickableViewAccessibility")
@Override//from   w  w  w  . ja  v a 2s.c om
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        zoom100X = (int) event.getRawX();
        zoom100Y = (int) event.getRawY();
        zoomHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                zoom100X = 0;
                zoom100Y = 0;
            }
        }, 2000);
        break;
    case MotionEvent.ACTION_UP:
        int dx = Math.abs((int) event.getRawX() - zoom100X);
        int dy = (int) event.getRawY() - zoom100Y;
        int h = v.getHeight();
        int w = v.getWidth() >> 1;
        if (dy > h * 0.8 && dy < h * 2 && dx < w) {
            wait(new Waitable() {
                @Override
                public void waitFor() {
                    synchronized (map) {
                        if (application.setZoom(1.)) {
                            map.updateMapInfo();
                            map.updateMapCenter();
                        }
                    }
                }
            });
            zoom100X = 0;
            zoom100Y = 0;
        }
        break;
    }
    return false;
}

From source file:android.support.v7.widget.RecyclerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (dispatchOnItemTouchIntercept(e)) {
        cancelTouch();//from ww w  .j a v  a 2 s .c  om
        return true;
    }

    final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
    final boolean canScrollVertically = mLayout.canScrollVertically();

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(e);

    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            getParent().requestDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
        if (index < 0) {
            Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
        }

        final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
        final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
        if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int dx = x - mInitialTouchX;
            final int dy = y - mInitialTouchY;
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
                mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
                startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
                mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
                startScroll = true;
            }
            if (startScroll) {
                getParent().requestDisallowInterceptTouchEvent(true);
                setScrollState(SCROLL_STATE_DRAGGING);
            }
        }
    }
        break;

    case MotionEventCompat.ACTION_POINTER_UP: {
        onPointerUp(e);
    }
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.clear();
    }
        break;

    case MotionEvent.ACTION_CANCEL: {
        cancelTouch();
    }
    }
    return mScrollState == SCROLL_STATE_DRAGGING;
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//  w  w w. j av a 2s  .  c o  m

    if (!mIsAttached) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
            return true;
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            boolean inList = false;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mXDown = ev.getX();//w  ww .  jav a2 s  . co  m
        mYDown = ev.getY();
        mTouchDownTime = System.currentTimeMillis();
        break;
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_REST) {
            final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
            if (currentPage != null && !currentPage.lastDownOnOccupiedCell()) {
                onWallpaperTap(ev);
            }
        }
    }
    return super.onInterceptTouchEvent(ev);
}

From source file:beichen.douban.ui.view.LazyViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from  ww w  .  ja v a2  s  .c  o  m*/
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have
         * caught it. Check whether the user has moved far enough from
         * his original down touch.
         */

        /*
         * Locally do absolute value. mLastMotionY is set to the y value
         * of the down event.
         */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on
            // content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = Math.abs(y - mLastMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it
            // be handled there.
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
        } else {
            if (yDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag... abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch. ACTION_DOWN always refers to
         * pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
}