Example usage for android.view MotionEvent ACTION_POINTER_INDEX_MASK

List of usage examples for android.view MotionEvent ACTION_POINTER_INDEX_MASK

Introduction

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

Prototype

int ACTION_POINTER_INDEX_MASK

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

Click Source Link

Document

Bits in the action code that represent a pointer index, used with #ACTION_POINTER_DOWN and #ACTION_POINTER_UP .

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();//from  w w w  .j a  va2  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: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();// w  ww . j  a  v a 2s  .c o  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;/*from   w w  w.  ja va 2  s  .c o m*/
    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);
}

From source file:com.nextgis.maplibui.fragment.ReorderedLayerView.java

@Override
public boolean onTouchEvent(@NonNull 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;/*from   w  ww .  ja  va  2  s  . c o  m*/
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == NOT_FOUND) {
            break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

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

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

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        ((LayersListAdapter) getAdapter()).notifyDataChanged();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        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();
        }
        setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:com.example.customview.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;/*from w  w w .  j  a  va2s. c o m*/
    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();
        drawLayoutSetUnlock();
        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();
        }
        drawLayoutSetUnlock();
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:com.spatialnetworks.fulcrum.widget.DynamicListView.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);

        /*//from   w  ww.  j  av a 2  s  .c o m
         * get the touch position. if it's valid and drag and drop is enabled, then get the view
         * and determine if the touch was on the drag and drop view. if so, set up for dragging
         */
        int position = pointToPosition((int) event.getX(), (int) event.getY());
        if (position != INVALID_POSITION && mDragAndDropEnabled) {
            View downView = getChildAt(position - getFirstVisiblePosition());
            assert downView != null;
            if (onDraggable(downView, event.getX() - downView.getX(), event.getY() - downView.getY())) {
                setUpDrag();
            }
        }

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

From source file:ucsc.hci.rankit.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);

        // This part has been copied from OnItemLongClickListener code above (which has now
        // been commented out).
        mTotalOffset = 0;// w  ww .ja v a2 s.  c o m

        int position = pointToPosition(mDownX, mDownY);
        int itemNum = position - getFirstVisiblePosition();

        View selectedView = getChildAt(itemNum);
        mMobileItemId = getAdapter().getItemId(position);
        mHoverCell = getAndAddHoverView(selectedView);
        selectedView.setVisibility(INVISIBLE);

        mCellIsMobile = true;

        updateNeighborViewsForID(mMobileItemId);
        // End of copy-paste from OnItemLongClickListener

        break;
    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);
            try {
                mHoverCell.setBounds(mHoverCellCurrentBounds);
            } catch (Exception e) {
                e.printStackTrace();
            }
            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);
}