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.albedinsky.android.support.ui.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 av  a 2s  . c  o  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) {
            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:ayushi.view.customview.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
 *//*w  w  w .j a  v a2  s . c  om*/
public boolean shouldInterceptTouchEvent(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);
        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 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) {
            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 = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            if (pointerId >= mInitialMotionX.length || pointerId >= mInitialMotionY.length) {
                continue;
            }
            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) mInitialMotionX[pointerId],
                    (int) mInitialMotionY[pointerId]);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

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

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

    return mDragState == STATE_DRAGGING;
}

From source file:cn.zmdx.kaka.locker.widget.SlidingPaneLayout.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());
        }/*  www  .jav a2 s  .  co  m*/
    }

    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 deltaX = x - mInitialMotionX;
        final float ady = Math.abs(y - mInitialMotionY);
        final int slop = mDragHelper.getTouchSlop();
        if ((adx > slop && ady > adx) || deltaX < 0) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
    }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}

From source file:illab.nabal.NabalSimpleDemoActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nabal_simple_demo);

    CookieSyncManager.createInstance(NabalSimpleDemoActivity.this);

    mBackground = (RelativeLayout) findViewById(R.id.relativeLayout1);

    mImgBtn1 = (ImageButton) findViewById(R.id.imageButton1);
    mImgBtn2 = (ImageButton) findViewById(R.id.imageButton2);
    mImgBtn3 = (ImageButton) findViewById(R.id.imageButton3);
    mImgBtn4 = (ImageButton) findViewById(R.id.imageButton4);
    mImgBtn5 = (ImageButton) findViewById(R.id.imageButton5);
    mImgBtn7 = (ImageButton) findViewById(R.id.imageButton7);
    mImgBtn8 = (ImageButton) findViewById(R.id.imageButton8);
    mEditText1 = (EditText) findViewById(R.id.editText1);
    mEditText2 = (EditText) findViewById(R.id.editText2);
    mEditText3 = (EditText) findViewById(R.id.editText3);

    mDialogHelper.toast("Please log in first.");

    mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    mInputMethodManager.hideSoftInputFromWindow(mEditText1.getWindowToken(), 0);
    mInputMethodManager.hideSoftInputFromWindow(mEditText2.getWindowToken(), 0);

    // set a listener to background for a fling
    mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override// w w  w.j a  va 2  s.  c  om
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                        toggleSnsButtons(SocialNetwork.WEIBO);
                    } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                        toggleSnsButtons(SocialNetwork.FACEBOOK);
                    } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                        toggleSnsButtons(SocialNetwork.TWITTER);
                    }
                }
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                        toggleSnsButtons(SocialNetwork.TWITTER);
                    } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                        toggleSnsButtons(SocialNetwork.WEIBO);
                    } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                        toggleSnsButtons(SocialNetwork.FACEBOOK);
                    }
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    });
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return mGestureDetector.onTouchEvent(event);
        }
    };
    mBackground.setOnTouchListener(gestureListener);

    // set a listener to background for a long-click
    mBackground.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Log.i(TAG, "#### background area long-clicked");
            if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                if (mFbBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mFbBgBitmap).show();
                }
            } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                if (mTwBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mTwBgBitmap).show();
                }
            } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                if (mWeBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mWeBgBitmap).show();
                }
            }
            return false;
        }
    });

    // set a listener to Facebook button
    mImgBtn3.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Facebook button clicked");
            toggleSnsButtons(SocialNetwork.FACEBOOK);
        }
    });

    // set a listener to Twitter button
    mImgBtn4.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Twitter button clicked");
            toggleSnsButtons(SocialNetwork.TWITTER);
        }
    });

    // set a listener to Weibo button
    mImgBtn5.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Weibo button clicked");
            toggleSnsButtons(SocialNetwork.WEIBO);
        }
    });

    // set a listener to portrait button
    mImgBtn1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### portrait button clicked");
            clearOutProfileUi();
            clearProfileBufferData(mCurrentSnsId);
            mHasErrorOccurredWhileFetchingProfile = false;
            mImgBtn1.setVisibility(View.GONE);
            requestMyProfile();
            requestMyLastStatus();
        }
    });
    mImgBtn1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View paramView) {
            //Log.i(TAG, "#### portrait button long-clicked");
            confirmJumpToSnsProfile();
            return true;
        }
    });

    // set a listener to photo attach button
    mImgBtn7.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### photo attach button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
                mEditText1.setText("");
                mDialogHelper.toast("Please log in first.");
            }

            // if logged-in already
            else {
                final String message = mEditText1.getText().toString();
                if (StringHelper.isEmpty(message) == true) {
                    mDialogHelper.toast("Please enter the photo description correctly.");
                } else {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, REQ_CODE_FETCH_BITMAP_FROM_GALLERY);
                }
            }
        }
    });

    // set a listener to status update button
    mImgBtn2.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### status update button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
                mEditText1.setText("");
                mDialogHelper.toast("Please log in first.");
            }

            // if logged-in already
            else {
                final String message = mEditText1.getText().toString();
                if (StringHelper.isEmpty(message) == true) {
                    mDialogHelper.toast("Please enter your status correctly.");
                } else {
                    postStatus(message);
                }
            }
        }
    });

    // set a listener to link button
    mImgBtn8.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### photo attach button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
            }

            // if logged-in already
            else {
                confirmJumpToLinkDialog();
            }
        }
    });
}

From source file:xiaofan.llongimageview.view.SubsamplingScaleImageView.java

private void setGestureDetector(final Context context) {
    this.detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override//from w w  w  . j a v  a  2 s.  c o m
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (panEnabled && readySent && vTranslate != null && e1 != null && e2 != null
                    && (Math.abs(e1.getX() - e2.getX()) > 50 || Math.abs(e1.getY() - e2.getY()) > 50)
                    && (Math.abs(velocityX) > 500 || Math.abs(velocityY) > 500) && !isZooming) {
                PointF vTranslateEnd = new PointF(vTranslate.x + (velocityX * 0.25f),
                        vTranslate.y + (velocityY * 0.25f));
                float sCenterXEnd = ((getWidth() / 2) - vTranslateEnd.x) / scale;
                float sCenterYEnd = ((getHeight() / 2) - vTranslateEnd.y) / scale;
                new AnimationBuilder(new PointF(sCenterXEnd, sCenterYEnd)).withEasing(EASE_OUT_QUAD)
                        .withPanLimited(false).start();
                return true;
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            performClick();
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (zoomEnabled && readySent && vTranslate != null) {
                float doubleTapZoomScale = Math.min(maxScale,
                        SubsamplingScaleImageView.this.doubleTapZoomScale);
                boolean zoomIn = scale <= doubleTapZoomScale * 0.9;
                float targetScale = zoomIn ? doubleTapZoomScale
                        : Math.min(getWidth() / (float) sWidth(), getHeight() / (float) sHeight());
                PointF targetSCenter = viewToSourceCoord(new PointF(e.getX(), e.getY()));
                if (doubleTapZoomStyle == ZOOM_FOCUS_CENTER_IMMEDIATE) {
                    setScaleAndCenter(targetScale, targetSCenter);
                } else if (doubleTapZoomStyle == ZOOM_FOCUS_CENTER || !zoomIn) {
                    new AnimationBuilder(targetScale, targetSCenter).withInterruptible(false).start();
                } else if (doubleTapZoomStyle == ZOOM_FOCUS_FIXED) {
                    new AnimationBuilder(targetScale, targetSCenter, new PointF(e.getX(), e.getY()))
                            .withInterruptible(false).start();
                }

                // Hacky solution for #15 - after a double tap the GestureDetector gets in a state where the next
                // fling is ignored, so here we replace it with a new one.
                setGestureDetector(context);

                invalidate();
                return true;
            }
            return super.onDoubleTapEvent(e);
        }
    });
}

From source file:ayushi.view.customview.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) {
            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) mInitialMotionX[pointerId],
                        (int) mInitialMotionY[pointerId]);
                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:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mLeftDragger.processTouchEvent(ev);/*from  www  .  j  a  va 2s . c om*/
    mRightDragger.processTouchEvent(ev);

    final int action = ev.getAction();
    boolean wantTouchEvents = true;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.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: {
        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: {
        closeDrawers(true);
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }
    }

    return wantTouchEvents;
}

From source file:android.wuliqing.com.lendphonesystemapp.swipeBack.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*from  ww  w.j a  v a2 s . c o  m*/
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(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);
        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 = mInitialEdgeTouched[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) {
            final int edgesTouched = mInitialEdgeTouched[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 = 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 (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

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

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

    return mDragState == STATE_DRAGGING;
}

From source file:cn.zmdx.kaka.locker.widget.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream./*  w w w  . j a v a 2s  . c om*/
 * 
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 *         onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(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);
        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 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) {
            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 = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; 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) mInitialMotionX[pointerId],
                    (int) mInitialMotionY[pointerId]);
            if (toCapture != null && checkTouchSlop(toCapture, dx, dy)
                    && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }

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

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

    return mDragState == STATE_DRAGGING;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    Log.i(TAG, "onInterceptTouchEvent()");
    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: {
        Log.i(TAG, "onInterceptTouchEvent() -- ACTION_DOWN");
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;/*from w  ww  . j a  v  a 2  s.c o m*/
        mInitialMotionY = y;
        if (mScrimOpacity > 0 && isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
            interceptForTap = true;
        }
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }

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

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        closeDrawers(true);
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
    }
    }
    boolean result = interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
    Log.i(TAG, "onInterceptTouchEvent() -- result = " + result);
    return result;
}