Example usage for android.view MotionEvent ACTION_POINTER_INDEX_SHIFT

List of usage examples for android.view MotionEvent ACTION_POINTER_INDEX_SHIFT

Introduction

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

Prototype

int ACTION_POINTER_INDEX_SHIFT

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

Click Source Link

Document

Bit shift for the action bits holding the pointer index as defined by #ACTION_POINTER_INDEX_MASK .

Usage

From source file:Main.java

@TargetApi(VERSION_CODES.HONEYCOMB)
private static int getPointerIndexHoneyComb(int action) {
    return (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
}

From source file:org.mozilla.gecko.ui.SimpleScaleGestureDetector.java

private int getActionIndex(MotionEvent event) {
    return (event.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
}

From source file:com.heinrichreimersoftware.materialintro.view.SwipeBlockableViewPager.java

private boolean handleTouchEvent(MotionEvent event) {
    boolean allowTouch = false;
    final int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        lastTouchX = event.getX();// ww w.j a  va 2  s. c  om

        // Save the ID of this pointer
        activePointerId = event.getPointerId(0);

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // Find the index of the active pointer and fetch its position
        final int pointerIndex = event.findPointerIndex(activePointerId);
        final float x = event.getX(pointerIndex);

        final float dx = x - lastTouchX;

        if (dx > 0) {
            // Swiped right
            if (!swipeRightEnabled && Math.abs(dx) > SWIPE_LOCK_THRESHOLD) {
                lockedRight = true;
            }
            if (!lockedRight) {
                allowTouch = true;
                if (Math.abs(dx) > SWIPE_UNLOCK_THRESHOLD) {
                    lockedLeft = false;
                }
            }
        } else if (dx < 0) {
            // Swiped left
            if (!swipeLeftEnabled && Math.abs(dx) > SWIPE_LOCK_THRESHOLD) {
                lockedLeft = true;
            }
            if (!lockedLeft) {
                allowTouch = true;
                if (Math.abs(dx) > SWIPE_UNLOCK_THRESHOLD) {
                    lockedRight = false;
                }
            }
        }

        lastTouchX = x;

        invalidate();
        break;
    }

    case MotionEvent.ACTION_UP: {
        activePointerId = INVALID_POINTER_ID;
        lockedLeft = false;
        lockedRight = false;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        activePointerId = INVALID_POINTER_ID;
        lockedLeft = false;
        lockedRight = false;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        // Extract the index of the pointer that left the touch sensor
        final int pointerIndex = (action
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == activePointerId) {
            // This was our active pointer going up. Choose a new
            // active pointer and adjust accordingly.
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            lastTouchX = event.getX(newPointerIndex);
            activePointerId = event.getPointerId(newPointerIndex);
        }
        break;
    }
    }

    return (!lockedLeft && !lockedRight) || allowTouch;
}

From source file:com.android.cts.uiautomator.TestGenericDetailFragment.java

private int getPointerIndex(MotionEvent event) {
    return ((event.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT);
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

private boolean move(MotionEvent motionEvent) {
    if (mSwipeView == null || mVelocityTracker == null || mPaused) {
        return false;
    }//from ww  w  .j  a v  a  2s .c  o m

    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
        mSwiping = true;
        mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        mSwipeView.setPressed(false);

        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mSwipeView.onTouchEvent(cancelEvent);
    }

    //Prevent swipes to disallowed directions
    if ((deltaX < 0 && mAllowedSwipeDirection == SwipeDirection.RIGHT)
            || (deltaX > 0 && mAllowedSwipeDirection == SwipeDirection.LEFT)) {
        resetMotion();
        return false;
    }

    if (mSwiping) {
        mTranslationX = deltaX;
        ViewCompat.setTranslationX(mSwipeView, deltaX - mSwipingSlop);
        ViewCompat.setAlpha(mSwipeView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        return true;
    }
    return false;
}

From source file:com.nononsenseapps.feeder.ui.SwipeDismissTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // offset because the view is translated during swipe
    motionEvent.offsetLocation(mTranslationX, 0);

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();//from ww  w. j  a  va2 s . c  o  m
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        // TODO: ensure this is a finger, and set a flag
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        if (mCallbacks.canDismiss(mToken)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        return false;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            mSwipingView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth)
                    //.alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss();
                        }
                    });
        } else if (mSwiping) {
            // cancel
            mSwipingView.animate().translationX(0)
                    //.alpha(1)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mCallbacks.onSwipeCancelled();
                        }
                    });
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        mSwipingView.animate().translationX(0)
                //.alpha(1)
                .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mCallbacks.onSwipeCancelled();
                    }
                });
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mView.getParent().requestDisallowInterceptTouchEvent(true);

            // Cancel listview's touch
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            mView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            if (notNotifiedSwipeStart) {
                notNotifiedSwipeStart = false;
                mCallbacks.onSwipeStarted(deltaX > 0);
            }
            mTranslationX = deltaX;
            mSwipingView.setTranslationX(deltaX - mSwipingSlop);
            //mView.setAlpha(mInterpolator.getInterpolation(1f - 1f * Math.abs(deltaX) / mViewWidth));
            //                    mView.setAlpha(Math.max(0f, Math.min(1f,
            //                            1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_CANCEL:
        Log.d(TAG, "Cancel Event");
        break;/* ww w  .j a  v a  2 s.c  o  m*/
    case MotionEvent.ACTION_DOWN:
        Log.d(TAG, "Press Event");
        break;
    case MotionEvent.ACTION_UP:
        Log.d(TAG, "Up Event");

        if (mVelocityTracker == null) {
            Log.d(TAG, "velocity tracker is null in action up");
            break;
        }

        Log.d(TAG, "Up Intercept");
        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        mVelocityTracker.addMovement(e);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (absDeltaX > mViewWidth / 2) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss) {
            dismiss(mChildView, mChildPosition, dismissRight);
        } else {
            mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null);
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    case MotionEvent.ACTION_MOVE:
        Log.d(TAG, "Move Event");
        mRecyclerView.requestDisallowInterceptTouchEvent(true);
        mRefreshLayout.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(e);
        cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL | (e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mRecyclerView.onTouchEvent(cancelEvent);
        mRefreshLayout.onTouchEvent(cancelEvent);
        cancelEvent.recycle();

        mChildView.setTranslationX(mDeltaX);
        /*mChildView.setAlpha(Math.max(0.15f, Math.min(1f,
                                                     1f - 2f * Math.abs(mDeltaX) /
                                                          mViewWidth)));*/
        break;
    }
}

From source file:com.haarman.listviewanimations.itemmanipulation.SwipeDismissListViewTouchListener.java

private boolean handleMoveEvent(MotionEvent motionEvent) {
    if (mPaused || (mVelocityTracker == null)) {
        return false;
    }//from   w  w  w  .j  a va  2  s . c  om

    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (mTouchChildTouched && !mDisallowSwipe && Math.abs(deltaX) > mSlop
            && Math.abs(deltaX) > Math.abs(deltaY)) {
        mSwiping = true;
        mListView.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mListView.onTouchEvent(cancelEvent);
    }

    if (mSwiping) {
        if (!mSwipeInitiated) {
            Log.d("SwipeDismissListViewTouchListener", "swipe/begin");
        }
        mSwipeInitiated = true;
        ViewHelper.setTranslationX(mCurrentDismissData.view, deltaX);
        ViewHelper.setAlpha(mCurrentDismissData.view,
                Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        for (View v : getAllTreeChildViews(mCurrentDismissData.view)) {
            ViewHelper.setTranslationX(v, deltaX);
            ViewHelper.setAlpha(v, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        }
        return true;
    }
    return false;
}

From source file:it.sephiroth.android.library.imagezoom.ScaleGestureDetector.java

public boolean onTouchEvent(final MotionEvent event) {
    final int action = event.getAction();
    final boolean handled = true;

    if (!mGestureInProgress) {
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_POINTER_DOWN: {
            // We have a new multi-finger gesture

            // as orientation can change, query the metrics in touch
            // down
            final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
            mRightSlopEdge = metrics.widthPixels - mEdgeSlop;
            mBottomSlopEdge = metrics.heightPixels - mEdgeSlop;

            // Be paranoid in case we missed an event
            reset();//from  w  w w  . j a  va 2s.  co m

            mPrevEvent = MotionEvent.obtain(event);
            mTimeDelta = 0;

            setContext(event);

            // Check if we have a sloppy gesture. If so, delay
            // the beginning of the gesture until we're sure that's
            // what the user wanted. Sloppy gestures can happen if the
            // edge of the user's hand is touching the screen, for
            // example.
            final float edgeSlop = mEdgeSlop;
            final float rightSlop = mRightSlopEdge;
            final float bottomSlop = mBottomSlopEdge;
            final float x0 = event.getRawX();
            final float y0 = event.getRawY();
            final float x1 = getRawX(event, 1);
            final float y1 = getRawY(event, 1);

            final boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop;
            final boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop;

            if (p0sloppy && p1sloppy) {
                mFocusX = -1;
                mFocusY = -1;
                mSloppyGesture = true;
            } else if (p0sloppy) {
                mFocusX = MotionEventCompat.getX(event, 1);
                mFocusY = MotionEventCompat.getY(event, 1);
                mSloppyGesture = true;
            } else if (p1sloppy) {
                mFocusX = MotionEventCompat.getX(event, 0);
                mFocusY = MotionEventCompat.getY(event, 0);
                mSloppyGesture = true;
            } else {
                mGestureInProgress = mListener.onScaleBegin(this);
            }
        }
            break;

        case MotionEvent.ACTION_MOVE:
            if (mSloppyGesture) {
                // Initiate sloppy gestures if we've moved outside of
                // the slop area.
                final float edgeSlop = mEdgeSlop;
                final float rightSlop = mRightSlopEdge;
                final float bottomSlop = mBottomSlopEdge;
                final float x0 = event.getRawX();
                final float y0 = event.getRawY();
                final float x1 = getRawX(event, 1);
                final float y1 = getRawY(event, 1);

                final boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop;
                final boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop;

                if (p0sloppy && p1sloppy) {
                    mFocusX = -1;
                    mFocusY = -1;
                } else if (p0sloppy) {
                    mFocusX = MotionEventCompat.getX(event, 1);
                    mFocusY = MotionEventCompat.getY(event, 1);
                } else if (p1sloppy) {
                    mFocusX = MotionEventCompat.getX(event, 0);
                    mFocusY = MotionEventCompat.getY(event, 0);
                } else {
                    mSloppyGesture = false;
                    mGestureInProgress = mListener.onScaleBegin(this);
                }
            }
            break;

        case MotionEvent.ACTION_POINTER_UP:
            if (mSloppyGesture) {
                // Set focus point to the remaining finger
                final int id = (action
                        & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT == 0
                                ? 1
                                : 0;
                mFocusX = event.getX(id);
                mFocusY = event.getY(id);
            }
            break;
        }
    } else {
        // Transform gesture in progress - attempt to handle it
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_POINTER_UP:
            // Gesture ended
            setContext(event);

            // Set focus point to the remaining finger
            final int id = (action
                    & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT == 0 ? 1
                            : 0;
            mFocusX = event.getX(id);
            mFocusY = event.getY(id);

            if (!mSloppyGesture) {
                mListener.onScaleEnd(this);
            }

            reset();
            break;

        case MotionEvent.ACTION_CANCEL:
            if (!mSloppyGesture) {
                mListener.onScaleEnd(this);
            }

            reset();
            break;

        case MotionEvent.ACTION_MOVE:
            setContext(event);

            // Only accept the event if our relative pressure is within
            // a certain limit - this can help filter shaky data as a
            // finger is lifted.
            if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) {
                final boolean updatePrevious = mListener.onScale(this);

                if (updatePrevious) {
                    mPrevEvent.recycle();
                    mPrevEvent = MotionEvent.obtain(event);
                }
            }
            break;
        }
    }
    return handled;
}

From source file:com.dgnt.dominionCardPicker.view.DynamicListView.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);
        break;//w  w  w .  j  av a 2 s . com
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;

        if (mCellIsMobile) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            mHoverCell.setBounds(mHoverCellCurrentBounds);
            invalidate();

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        /* If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview. */
        pointerIndex = (event.getAction()
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}