Example usage for android.view MotionEvent getActionIndex

List of usage examples for android.view MotionEvent getActionIndex

Introduction

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

Prototype

public final int getActionIndex() 

Source Link

Document

For #ACTION_POINTER_DOWN or #ACTION_POINTER_UP as returned by #getActionMasked , this returns the associated pointer index.

Usage

From source file:android.support.design.widget.BottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*  w w  w.j a  v a 2  s .co m*/
    int action = MotionEventCompat.getActionMasked(event);
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

From source file:net.osmand.plus.widgets.tools.ExtendedBottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*w  w w .  j a  v a  2s.  c o  m*/
    int action = event.getActionMasked();
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

From source file:android.support.designox.widget.BottomSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//from  w  w w .  ja  va2 s.co  m
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();
        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }
        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:com.devexweb.googlemapsapi.lib.BottomSheetBehaviorGoogleMapsLike.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*from   w ww  .j a v  a  2  s.c om*/
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();
        if (mState == STATE_ANCHOR_POINT) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        } else {
            View scroll = mNestedScrollingChildRef.get();
            if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
                mActivePointerId = event.getPointerId(event.getActionIndex());
                mTouchingScrollingChild = true;
            }
        }
        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:com.commonsware.cwac.crossport.design.widget.BottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*from w  w w. j a v a 2 s  . c  o  m*/
    int action = event.getActionMasked();
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    if (mViewDragHelper != null) {
        mViewDragHelper.processTouchEvent(event);
    }
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

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

private int getValue(MotionEvent event, Thumb thumb) {
    return getValue(event, event.getActionIndex(), thumb);
}

From source file:io.authme.sdk.widget.LockPatternView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!mInputEnabled || !isEnabled()) {
        return false;
    }//from w  w w  . j  a v a  2s.  co  m

    if (event.getAction() == MotionEvent.ACTION_MOVE && collectSensor == true) {
        float x = event.getX();
        float y = event.getY();
        RawXY xy = new RawXY(x, y, event.getEventTime());
        rawXYList.add(xy);
    }

    int index = event.getActionIndex();
    int pointerId = event.getPointerId(index);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        handleActionDown(event);
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {
            mVelocityTracker.clear();
        }
        mVelocityTracker.addMovement(event);
        return true;
    case MotionEvent.ACTION_UP:
        handleActionUp(event);
        return true;
    case MotionEvent.ACTION_MOVE:
        handleActionMove(event);
        mVelocityTracker.addMovement(event);
        mVelocityTracker.computeCurrentVelocity(1000);
        float x = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
        float y = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);
        Velocity xy = new Velocity(x, y, event.getEventTime());
        velocityList.add(xy);
        return true;
    case MotionEvent.ACTION_CANCEL:
        /*
         * Original source check for mPatternInProgress == true first before
         * calling next three lines. But if we do that, there will be
         * nothing happened when the user taps at empty area and releases
         * the finger. We want the pattern to be reset and the message will
         * be updated after the user did that.
         */
        mVelocityTracker.recycle();
        mPatternInProgress = false;
        resetPattern();
        notifyPatternCleared();

        if (PROFILE_DRAWING) {
            if (mDrawingProfilingStarted) {
                Debug.stopMethodTracing();
                mDrawingProfilingStarted = false;
            }
        }
        return true;
    }
    return false;
}

From source file:com.hardsoftstudio.anchorbottomsheet.AnchorSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown() || !isEnabled) {
        return false;
    }/*from  w w w  .j  av a 2 s. c o m*/
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();
        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }
        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:com.geely.testbottomsheets.CustomBottomSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*from w w  w.  j a  v  a 2 s . c  o  m*/
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();

        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }

        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:com.example.SmartBoard.DrawingView.java

public boolean onTouchDragEvent(MotionEvent event) {

    boolean handled = false;

    JSONObject mObjectTouched;/*from   w ww.j a  v  a 2  s.c  o  m*/
    int X;
    int Y;
    int pointerId;
    int actionIndex = event.getActionIndex();

    //get coordinates and make object transparent
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        dragFinished = false;

        //it's the first pointer, so clear all existing pointers data
        mObjectPointers.clear();
        X = (int) event.getX(actionIndex);
        Y = (int) event.getY(actionIndex);
        dragX = X;
        dragY = Y;
        String objectType = "";
        //check if we've touched inside some object
        mObjectTouched = getTouchedObject(X, Y);

        if (mObjectTouched == null) {
            return true;
        } else {
            objectType = getObjectType(mObjectTouched);
        }

        if (objectType.compareTo("Circle") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);

            } catch (JSONException e) {
                e.printStackTrace();

            }
        } else if (objectType.compareTo("Rectangle") == 0) {
            Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
            tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
            try {
                mObjectTouched.put("dimens", tempRect.flattenToString());
            } catch (JSONException e) {

            }

        } else if (objectType.compareTo("Line") == 0) {

            if (lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                    mObjectTouched.optInt("starty")) < lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                            mObjectTouched.optInt("stopy"))) {

                try {
                    mObjectTouched.put("startx", X);
                    mObjectTouched.put("starty", Y);
                    mObjectTouched.put("length",
                            lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = true;
            } else {
                try {
                    mObjectTouched.put("stopx", X);
                    mObjectTouched.put("stopy", Y);
                    mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                            mObjectTouched.optInt("starty")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = false;
            }

        } else if (objectType.compareTo("Text") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);
                Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                mObjectTouched.put("region", tempRect.flattenToString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        mObjectPointers.put(event.getPointerId(0), mObjectTouched);
        if (mObjectTouched != null) {
            mqtt.publishObject(mObjectTouched);
        }
        invalidate();
        handled = true;
        break;

    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_MOVE:
        dragFinished = false;

        final int pointerCount = event.getPointerCount();

        for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) {
            //some pointer has moved, search it by pointer id
            pointerId = event.getPointerId(actionIndex);
            X = (int) event.getX(actionIndex);
            Y = (int) event.getY(actionIndex);

            dragX = X;
            dragY = Y;

            mObjectTouched = mObjectPointers.get(pointerId);
            if (mObjectTouched == null)
                continue; // if null no object was touched so skip

            if (mObjectTouched.optString("type").compareTo("Circle") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                } catch (JSONException e) {
                }
            } else if (mObjectTouched.optString("type").compareTo("Rectangle") == 0) {

                Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
                tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
                try {
                    mObjectTouched.put("dimens", tempRect.flattenToString());
                } catch (JSONException e) {

                }

            } else if (mObjectTouched.optString("type").compareTo("Text") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                    Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                    tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                    mObjectTouched.put("region", tempRect.flattenToString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }

            else if (mObjectTouched.optString("type").compareTo("Line") == 0) {

                if (dragLineStart) {
                    try {
                        mObjectTouched.put("startx", X);
                        mObjectTouched.put("starty", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                                mObjectTouched.optInt("stopy")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        mObjectTouched.put("stopx", X);
                        mObjectTouched.put("stopy", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                                mObjectTouched.optInt("starty")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }

            if (mObjectTouched != null) {
                mqtt.publishObject(mObjectTouched);
            }
        }

        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_UP:
        dragFinished = true;

        mObjectPointers.clear();
        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_CANCEL:
        handled = true;
        break;

    default:
        // do nothing
        break;
    }

    return super.onTouchEvent(event) || handled;
}