Example usage for android.view MotionEvent getPointerCount

List of usage examples for android.view MotionEvent getPointerCount

Introduction

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

Prototype

public final int getPointerCount() 

Source Link

Document

The number of pointers of data contained in this event.

Usage

From source file:com.bluetech.gallery5.ui.ImageDetailFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /*/*from   ww w.  j  ava  2 s  .  c  om*/
    // Inflate and locate the main ImageView
    final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageRecycView);
    return v;
    */
    final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageRecycView);
    //ImageView img = new ImageView(getActivity());

    mImageView.setPadding(6, 6, 6, 6);

    // mImageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)) ;
    //ImageViewHelperURL.setUrlDrawable((ImageView) img, url, R.drawable.no_image) ;

    mImageView.setOnTouchListener(new View.OnTouchListener() {
        private static final String TAG = "SlideImageView";
        // These matrices will be used to move and zoom image
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();

        // We can be in one of these 3 states
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;

        // Remember some things for zooming
        PointF start = new PointF();
        PointF mid = new PointF();
        float oldDist = 1f;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;

            // Dump touch event to log
            dumpEvent(event);

            // Handle touch events here...
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG");
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM");
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                Log.d(TAG, "mode=NONE");
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // ...
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        Log.d(TAG, "ZOOOOOOOM: " + scale);
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
            }

            view.setImageMatrix(matrix);
            return true; // indicate event was handled
        }

        /** Show an event in the LogCat view, for debugging */
        private void dumpEvent(MotionEvent event) {
            String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?",
                    "8?", "9?" };
            StringBuilder sb = new StringBuilder();
            int action = event.getAction();
            int actionCode = action & MotionEvent.ACTION_MASK;
            sb.append("event ACTION_").append(names[actionCode]);
            if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
                sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
                sb.append(")");
            }
            sb.append("[");
            for (int i = 0; i < event.getPointerCount(); i++) {
                sb.append("#").append(i);
                sb.append("(pid ").append(event.getPointerId(i));
                sb.append(")=").append((int) event.getX(i));
                sb.append(",").append((int) event.getY(i));
                if (i + 1 < event.getPointerCount())
                    sb.append(";");
            }
            sb.append("]");
            Log.d(TAG, sb.toString());
        }

        /** Determine the space between the first two fingers */
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return (float) Math.sqrt(x * x + y * y);
        }

        /** Calculate the mid point of the first two fingers */
        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    });

    return v;

}

From source file:com.cssweb.android.view.KlineView.java

/**
 * true?,false??/*from   w  ww. java  2s . c  o m*/
 */
public boolean onTouchEvent(MotionEvent motionEvent) {
    int count = motionEvent.getPointerCount();
    if (count == 2) {//
        isTrackStatus = false;
        if (motionEvent.getAction() == MotionEvent.ACTION_POINTER_1_DOWN
                || motionEvent.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
            float x0 = motionEvent.getX(0);
            float x1 = motionEvent.getX(1);
            float y0 = motionEvent.getY(0);
            float y1 = motionEvent.getY(1);
            distanceY0 = Math.abs(y1 - y0);
            distanceX0 = Math.abs(x1 - x0);
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_POINTER_1_UP
                || motionEvent.getAction() == MotionEvent.ACTION_POINTER_2_UP) {
            float x0 = motionEvent.getX(0);
            float x1 = motionEvent.getX(1);
            float y0 = motionEvent.getY(0);
            float y1 = motionEvent.getY(1);
            distanceY1 = Math.abs(y1 - y0);
            distanceX1 = Math.abs(x1 - x0);
            if (distanceY1 > distanceY0 && distanceX1 > distanceX0) {
                upHandler();
            } else if (distanceY1 < distanceY0 && distanceX1 < distanceX0) {
                downHandler();
            }
        }
        return false;
    } else if (count == 1) {//??
        switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touchesBegan(motionEvent);
            break;

        case MotionEvent.ACTION_MOVE:
            touchesMoved(motionEvent);
            break;

        case MotionEvent.ACTION_UP:
            touchesEnded(motionEvent);
            break;

        }
    }
    return true;
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Here we actually handle the touch event (e.g. if the action is ACTION_MOVE,
    // scroll this container).
    // This method will only be called if the touch event was intercepted in
    // onInterceptTouchEvent

    if (DEBUG_TOUCH)
        Log.d(TAG, "onTouchEvent");

    if (!mDrawerState.mDraggingEnabled)
        return false;

    if (mVelocityTracker != null)
        mVelocityTracker.addMovement(ev);

    final int action = MotionEventCompat.getActionMasked(ev);

    if (!mIsScrolling) {
        if (DEBUG_TOUCH)
            Log.d(TAG, "NOT SCROLLING IS NOW SCROLLING");
        int drawer = preProcessScrollMotionEvent(ev);
        postProcessScrollMotionEvent(drawer, ev);
        if (action == MotionEvent.ACTION_UP) {
            if (mDrawerState.mScrollState == IDLE)
                processIdleUp(ev);/*from ww w  .  ja  va2 s  .  c  o m*/
        }
        return true; // needs to be true so we keep getting these values
    }

    if (DEBUG_TOUCH)
        Log.d(TAG, "DOING THE TOUCH STUFF");

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        mIsScrolling = false;
        mStartedTracking = false;
        snap();
        mVelocityTracker.clear();
    }

    if (action == MotionEvent.ACTION_MOVE) {
        int ptrIdx = ev.findPointerIndex(mDownEvent.mPointerId);
        if (DEBUG_TOUCH)
            Log.d(TAG, "ptrIdx: " + ptrIdx);
        if (ptrIdx >= ev.getPointerCount() || ptrIdx < 0)
            return true;

        if (mDrawerState.mActiveDrawer == LEFT_DRAWER || mDrawerState.mActiveDrawer == RIGHT_DRAWER) {
            mCurrentScrollX += mStartedTracking ? ev.getX(ptrIdx) - mLastMoveEvent.mCoords.x
                    : ev.getX(ptrIdx) - mDownEvent.mCoords.x;
            mCurrentScrollX = Math.max(mMinScrollX + mPeekSize.right, mCurrentScrollX);
            mCurrentScrollX = Math.min(mMaxScrollX - mPeekSize.left, mCurrentScrollX);
        } else if (mDrawerState.mActiveDrawer == TOP_DRAWER || mDrawerState.mActiveDrawer == BOTTOM_DRAWER) {
            mCurrentScrollY += mStartedTracking ? ev.getY(ptrIdx) - mLastMoveEvent.mCoords.y
                    : ev.getY(ptrIdx) - mDownEvent.mCoords.y;
            mCurrentScrollY = Math.max(mMinScrollY + mPeekSize.bottom, mCurrentScrollY);
            mCurrentScrollY = Math.min(mMaxScrollY - mPeekSize.top, mCurrentScrollY);
        }

        if (!mStartedTracking)
            mStartedTracking = true;
        mLastMoveEvent.mCoords.x = ev.getX(ptrIdx);
        mLastMoveEvent.mCoords.y = ev.getY(ptrIdx);

        // Log.d(TAG, "=======================");
        // Log.d(TAG, "mCurrentScrollX: "+mCurrentScrollX);
        // Log.d(TAG, "mCurrentScrollY: "+mCurrentScrollY);
        // Log.d(TAG, "mMinScrollY: "+mMinScrollY);
        // Log.d(TAG, "mMaxScrollY: "+mMaxScrollY);
        // Log.d(TAG, "mPeekSize.top: "+mPeekSize.top);
        // Log.d(TAG, "mPeekSize.bottom: "+mPeekSize.bottom);

        adjustScrollStates(mCurrentScrollX, mCurrentScrollY);
        notifyOffset();
    }

    return true;
}

From source file:com.wb.launcher3.Page.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = ev.getAction() & MotionEvent.ACTION_MASK;
    mPointCount = ev.getPointerCount();//added by zxa 1018 for adjust brightness

    //Log.i("zwb", "zhangwuba ------ luancher dispatchTouchEvent");

    boolean enable = (Settings.Secure.getInt(mContext.getContentResolver(), "tyd_gesture_launcher", 1) == 1);
    boolean gesture = false;

    if (mPointCount == 2) {
        int x1 = (int) ev.getX(0);
        int y1 = (int) ev.getY(0);

        int x2 = (int) ev.getX(1);
        int y2 = (int) ev.getY(1);

        x1Down = x1;/*  www  . j  a v a 2  s  .co  m*/
        x2Down = x2;
        y1Down = y1;
        y2Down = y2;

        xyD = (int) FloatMath.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        //Log.i("zwb", "zhangwuba ------  xyD = " + xyD);
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        //Log.i("xyz", "ACTION_DOWN");
        tempX = 0;
        tempY = 0;
        mOldY = ev.getY();
        mOldX = ev.getX();

        // mMode = DRAG;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        mLastMultiDownDistance = getDistance(ev);
        //mMode = ZOOM;
        break;
    case MotionEvent.ACTION_UP:
        //added by zxa 1018 for multi touch panel  adjust screen light  start
        if (enable) {
            closebrightnessDialog();
        }
        //added by zxa 1018 for multi touch panel  adjust screen light  end
        //mMode = NONE;
        break;
    case MotionEvent.ACTION_POINTER_UP:
        //added by zxa 1018 for multi touch panel  adjust screen light  start
        if (enable) {
            closebrightnessDialog();
        }
        //mMode = NONE;
        break;
    case MotionEvent.ACTION_MOVE:
        int scrollLength = xyD - temp;
        temp = xyD;
        if (enable && Math.abs(scrollLength) < 5 && y1Down != (int) mOldY) {
            adjustBrightness(ev, mPointCount);
        }
        /* if (mMode == ZOOM) {
        double currentLength = getDistance(ev);
        float length = (float) (currentLength - mLastMultiDownDistance);
        if (length < SHOW_WORKSPACE_PREVIEW_THRESHOLD) {
            onMultiZoomIn(length);
        } else {
            onMultiZoomOut(length);
        }
         }*/
        break;
    }

    /*  if (mMode == ZOOM) {
    return true;
      }*/

    return super.dispatchTouchEvent(ev);
}

From source file:com.wb.launcher3.Page.java

protected void onOtherPointerUp(MotionEvent ev) {
    int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        int lastIndex = ev.getPointerCount() - 0x1;
        final int newPointerIndex = pointerIndex == lastIndex ? (lastIndex - 0x1) : lastIndex;
        mLastMotionX = ev.getX(newPointerIndex);
        mLastMotionY = ev.getY(newPointerIndex);
        mLastMotionXRemainder = 0.0f;//from w  w  w.  j  a  va  2 s . c  o  m
        mActivePointerId = ev.getPointerId(newPointerIndex);
        releaseVelocityTracker();
    }
}

From source file:com.sun.toy.ux.widget.CustomViewPager.java

License:asdf

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from w w w . j  a  v  a 2  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;
    Log.i("asdfasdf", "sdlfjsd : " + ev.getY());
    if (mIsUnableToDrag) {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = ev.getRawY();
        final float yDiff = y - mLastMotionRawY;
        Log.i("viewpager.drag.point", ev.getPointerCount() + " || delta : " + yDiff);
        Log.e("viewpager.drag.point", "mInitialMotionY : " + mInitialMotionY + " | " + y);
        deliver.onDragY(yDiff);
    }
    // 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;
        mVelocityTracker.computeCurrentVelocity(1000);
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        deliver.onDragEnd();
        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!");
            mLastMotionRawY = ev.getRawY();
            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 - mInitialMotionY);
        mLastMotionRawY = ev.getRawY();
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        //
        //            if (dx != 0 && !isGutterDrag(mLastMotionX, dx) &&
        //                  canScroll(this, false, (int) dx, (int) x, (int) y)) {
        //               // Nested view has scrollable area under this point. Let it be handled there.
        //               mLastMotionX = x;
        //               mLastMotionY = y;
        //               mIsUnableToDrag = true;
        //               return false;
        //            }
        if (xDiff > mTouchSlop /*&& xDiff * 0.5f > yDiff*/) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
            mLastMotionY = y;
            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!");
            Log.d("viewpager", "drag y");
            //                    deliver.onDragY(yDiff);
            mIsUnableToDrag = true;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            if (performDrag(x)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        //                if (mIsUnableToDrag) {
        //                    deliver.onDragY(yDiff);
        //                }
        break;
    }

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

        mScroller.computeScrollOffset();
        if (mScrollState == SCROLL_STATE_SETTLING
                && Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
            // Let the user 'catch' the pager as it animates.
            mScroller.abortAnimation();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

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

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

    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;
}

From source file:com.wb.launcher3.Page.java

public boolean handleMotionEvent(MotionEvent ev) {
    if (getChildCount() <= 0) {
        return false;
    }/*w  ww  . j  av a  2  s  .co  m*/
    acquireVelocityTrackerAndAddMovement(ev);
    switch (MotionEvent.ACTION_MASK & ev.getAction()) {
    case MotionEvent.ACTION_POINTER_DOWN:
        if (ev.getPointerCount() <= 2) {
            if (!this.mScroller.isFinished()) {
                this.mScroller.abortAnimation();
            }
            int index = (ev.getAction()
                    & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            float px = ev.getX(index);
            mLastMotionX = px;
            mDownMotionX = px;
            mLastMotionY = ev.getY(index);
            mLastMotionXRemainder = 0.0F;
            mTotalMotionX = 0.0F;
            mActivePointerId = ev.getPointerId(index);
            mTouchState = TOUCH_STATE_SCROLLING;
            pageBeginMoving();
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            int pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex >= 0) {
                float x = ev.getX(pointerIndex);
                float deltaX = mLastMotionX + mLastMotionXRemainder - x;
                mTotalMotionX += Math.abs(deltaX);
                if (Math.abs(deltaX) >= 1.0F) {
                    mTouchX += deltaX;
                    mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                    if (!mDeferScrollUpdate) {
                        scrollBy((int) deltaX, 0);
                        if (DEBUG)
                            Log.d(TAG, "handleMotionEvent().Scrolling: " + deltaX);
                    } else {
                        invalidate();
                    }
                    mLastMotionX = x;
                    mLastMotionXRemainder = deltaX - (int) deltaX;
                } else {
                    awakenScrollBars();
                }
            }
        } else {
            determineScrollingStart(ev);
        }
        break;

    case MotionEvent.ACTION_POINTER_UP:
        if (ev.getPointerCount() > 2) {
            onOtherPointerUp(ev);
            break;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int activePointerId = mActivePointerId;
            final int pointerIndex = ev.findPointerIndex(activePointerId);
            final float x = ev.getX(pointerIndex);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
            final int deltaX = (int) (x - mDownMotionX);
            final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
            boolean isSignificantMove = Math.abs(deltaX) > pageWidth * SIGNIFICANT_MOVE_THRESHOLD;

            mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);

            boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING
                    && Math.abs(velocityX) > mFlingThresholdVelocity;
            boolean returnToOriginalPage = false;
            if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD
                    && Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
                returnToOriginalPage = true;
            }

            if (TydtechConfig.CYCLE_ROLL_PAGES_ENABLED) {
                m_moveNextDeltaX = deltaX;
                m_isSignificantMoveNext = (!returnToOriginalPage && (isSignificantMove || isFling));
            }

            int finalPage;
            if (((isSignificantMove && deltaX > 0 && !isFling) || (isFling && velocityX > 0))
                    && mCurrentPage > 0) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else if (((isSignificantMove && deltaX < 0 && !isFling) || (isFling && velocityX < 0))
                    && mCurrentPage < getChildCount() - 1) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
            int nextPage = Math.max(0, mCurrentPage - 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
            int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else {
            snapToDestination();
            onUnhandledTap(ev);
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;

    case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            snapToDestination();
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;
    }

    return true;
}

From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's onInterceptTouchEvent should
 * cause the parent to intercept the touch event stream.
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from onInterceptTouchEvent
 *//*from w ww. j av a  2  s . c  o m*/
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    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 = ev.getPointerId(0);
        saveInitialMotion(x, y, pointerId);

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

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerId = ev.getPointerId(actionIndex);
        final float x = ev.getX(actionIndex);
        final float y = ev.getY(actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // First to cross a touch slop over a draggable view wins. Also report edge drags.
        final int pointerCount = ev.getPointerCount();
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = ev.getPointerId(i);
            final float x = ev.getX(i);
            final float y = ev.getY(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 (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}

From source file:jackson.com.slidingmenulib.MyViewDragHelper.java

/**
 * Check if this event as provided to the parent view's onInterceptTouchEvent should
 * cause the parent to intercept the touch event stream.
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from onInterceptTouchEvent
 *///from   ww w  . ja v a2  s  .co m
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    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 = ev.getPointerId(0);
        saveInitialMotion(x, y, pointerId);

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

        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerId = ev.getPointerId(actionIndex);
        final float x = ev.getX(actionIndex);
        final float y = ev.getY(actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mInitialMotionX == null || mInitialMotionY == null)
            break;

        // First to cross a touch slop over a draggable view wins. Also report edge drags.
        final int pointerCount = ev.getPointerCount();
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = ev.getPointerId(i);

            // If pointer is invalid then skip the ACTION_MOVE.
            if (!isValidPointerForActionMove(pointerId))
                continue;

            final float x = ev.getX(i);
            final float y = ev.getY(i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];

            final View toCapture = findTopChildUnder((int) x, (int) y);
            final boolean pastSlop = toCapture != null && checkTouchSlop(toCapture, dx, dy);
            if (pastSlop) {
                // check the callback's
                // getView[Horizontal|Vertical]DragRange methods to know
                // if you can move at all along an axis, then see if it
                // would clamp to the same value. If you can't move at
                // all in every dimension with a nonzero range, bail.
                final int oldLeft = toCapture.getLeft();
                final int targetLeft = oldLeft + (int) dx;
                final int newLeft = mCallback.clampViewPositionHorizontal(toCapture, targetLeft, (int) dx);
                final int oldTop = toCapture.getTop();
                final int targetTop = oldTop + (int) dy;
                final int newTop = mCallback.clampViewPositionVertical(toCapture, targetTop, (int) dy);
                final int horizontalDragRange = mCallback.getViewHorizontalDragRange(toCapture);
                final int verticalDragRange = mCallback.getViewVerticalDragRange(toCapture);
                if ((horizontalDragRange == 0 || horizontalDragRange > 0 && newLeft == oldLeft)
                        && (verticalDragRange == 0 || verticalDragRange > 0 && newTop == oldTop)) {
                    break;
                }
            }
            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }

            if (pastSlop && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }

    return mDragState == STATE_DRAGGING;
}