Example usage for android.view MotionEvent findPointerIndex

List of usage examples for android.view MotionEvent findPointerIndex

Introduction

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

Prototype

public final int findPointerIndex(int pointerId) 

Source Link

Document

Given a pointer identifier, find the index of its data in the event.

Usage

From source file:Main.java

/**
 * Computes a Camera.Area corresponding to the new focus area to focus the camera on. This is
 * done by deriving a square around the center of a MotionEvent pointer (with side length equal
 * to FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH), then transforming this rectangle's/square's
 * coordinates into the (-1000, 1000) coordinate system used for camera focus areas.
 *
 * Also note that we operate on RectF instances for the most part, to avoid any integer
 * division rounding errors going forward. We only round at the very end for playing into
 * the final focus areas list./*w w  w.java 2 s .  com*/
 *
 * @throws RuntimeException if unable to compute valid intersection between MotionEvent region
 * and SurfaceTexture region.
 */
protected static Camera.Area computeFocusAreaFromMotionEvent(final MotionEvent event,
        final int surfaceTextureWidth, final int surfaceTextureHeight) {
    // Get position of first touch pointer.
    final int pointerId = event.getPointerId(0);
    final int pointerIndex = event.findPointerIndex(pointerId);
    final float centerX = event.getX(pointerIndex);
    final float centerY = event.getY(pointerIndex);

    // Build event rect. Note that coordinates increase right and down, such that left <= right
    // and top <= bottom.
    final RectF eventRect = new RectF(centerX - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // left
            centerY - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // top
            centerX + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // right
            centerY + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH // bottom
    );

    // Intersect this rect with the rect corresponding to the full area of the parent surface
    // texture, making sure we are not placing any amount of the eventRect outside the parent
    // surface's area.
    final RectF surfaceTextureRect = new RectF((float) 0, // left
            (float) 0, // top
            (float) surfaceTextureWidth, // right
            (float) surfaceTextureHeight // bottom
    );
    final boolean intersectSuccess = eventRect.intersect(surfaceTextureRect);
    if (!intersectSuccess) {
        throw new RuntimeException("MotionEvent rect does not intersect with SurfaceTexture rect; unable to "
                + "compute focus area");
    }

    // Transform into (-1000, 1000) focus area coordinate system. See
    // https://developer.android.com/reference/android/hardware/Camera.Area.html.
    // Note that if this is ever changed to a Rect instead of RectF, be cautious of integer
    // division rounding!
    final RectF focusAreaRect = new RectF((eventRect.left / surfaceTextureWidth) * 2000 - 1000, // left
            (eventRect.top / surfaceTextureHeight) * 2000 - 1000, // top
            (eventRect.right / surfaceTextureWidth) * 2000 - 1000, // right
            (eventRect.bottom / surfaceTextureHeight) * 2000 - 1000 // bottom
    );
    Rect focusAreaRectRounded = new Rect();
    focusAreaRect.round(focusAreaRectRounded);
    return new Camera.Area(focusAreaRectRounded, FOCUS_AREA_WEIGHT);
}

From source file:com.tsoliveira.android.listeners.DragDropTouchListener.java

private boolean move(MotionEvent event) {
    if (activePointerId == -1) {
        return false;
    }//from   ww  w.  java 2s  .  c o m

    int pointerIndex = event.findPointerIndex(activePointerId);
    int currentY = (int) event.getY(pointerIndex);
    int deltaY = currentY - downY;
    int mobileViewY = mobileViewStartY + deltaY;
    mobileView.setY(mobileViewY);

    switchViewsIfNeeded();
    scrollIfNeeded();
    return true;
}

From source file:io.mariachi.allianzvision.camera.api14.Camera1.java

public void handleFocus(MotionEvent event, Camera.Parameters params) {
    int pointerId = event.getPointerId(0);
    int pointerIndex = event.findPointerIndex(pointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);

    List<String> supportedFocusModes = params.getSupportedFocusModes();
    if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
        mCamera.autoFocus(new Camera.AutoFocusCallback() {
            @Override//  www. j ava 2s  .  c  o  m
            public void onAutoFocus(boolean b, Camera camera) {
                // currently set to auto-focus on single touch
            }
        });
    }
}

From source file:com.google.blockly.android.ui.PendingDrag.java

/**
 * Constructs a new PendingDrag that, if accepted by the DragHandler, begins with the
 * {@code actionDown} event./*from  w ww. j  a v  a2 s.co  m*/
 *
 * @param controller The activity's {@link BlocklyController}.
 * @param touchedView The initial touched {@link BlockView} of the drag.
 * @param actionDown The first {@link MotionEvent#ACTION_DOWN} event.
 */
PendingDrag(@NonNull BlocklyController controller, @NonNull BlockView touchedView,
        @NonNull MotionEvent actionDown) {
    if (actionDown.getAction() != MotionEvent.ACTION_DOWN) {
        throw new IllegalArgumentException();
    }

    mController = controller;
    mHelper = controller.getWorkspaceHelper();

    mLatestEventTime = actionDown.getEventTime();

    mTouchedView = touchedView;

    mPointerId = actionDown.getPointerId(actionDown.getActionIndex());
    int pointerIdx = actionDown.findPointerIndex(mPointerId);
    mTouchDownBlockX = (int) actionDown.getX(pointerIdx);
    mTouchDownBlockY = (int) actionDown.getY(pointerIdx);

    touchedView.getTouchLocationOnScreen(actionDown, mTouchDownScreen);
    mHelper.screenToWorkspaceCoordinates(mTouchDownScreen, mTouchDownWorkspace);

    mGestureDetector = new GestureDetectorCompat(mController.getContext(), new GestureListener());
    mGestureDetector.onTouchEvent(actionDown);
}

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();//w  w  w  . ja va  2  s  .  c o  m

        // 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:android.support.v7.widget.DropDownListView.java

/**
 * Handles forwarded events./*from  ww w  . j  a  va 2 s .c o m*/
 *
 * @param activePointerId id of the pointer that activated forwarding
 * @return whether the event was handled
 */
public boolean onForwardedEvent(MotionEvent event, int activePointerId) {
    boolean handledEvent = true;
    boolean clearPressedItem = false;

    final int actionMasked = MotionEventCompat.getActionMasked(event);
    switch (actionMasked) {
    case MotionEvent.ACTION_CANCEL:
        handledEvent = false;
        break;
    case MotionEvent.ACTION_UP:
        handledEvent = false;
        // $FALL-THROUGH$
    case MotionEvent.ACTION_MOVE:
        final int activeIndex = event.findPointerIndex(activePointerId);
        if (activeIndex < 0) {
            handledEvent = false;
            break;
        }

        final int x = (int) event.getX(activeIndex);
        final int y = (int) event.getY(activeIndex);
        final int position = pointToPosition(x, y);
        if (position == INVALID_POSITION) {
            clearPressedItem = true;
            break;
        }

        final View child = getChildAt(position - getFirstVisiblePosition());
        setPressedItem(child, position, x, y);
        handledEvent = true;

        if (actionMasked == MotionEvent.ACTION_UP) {
            clickPressedItem(child, position);
        }
        break;
    }

    // Failure to handle the event cancels forwarding.
    if (!handledEvent || clearPressedItem) {
        clearPressedItem();
    }

    // Manage automatic scrolling.
    if (handledEvent) {
        if (mScrollHelper == null) {
            mScrollHelper = new ListViewAutoScrollHelper(this);
        }
        mScrollHelper.setEnabled(true);
        mScrollHelper.onTouch(this, event);
    } else if (mScrollHelper != null) {
        mScrollHelper.setEnabled(false);
    }

    return handledEvent;
}

From source file:me.wizos.loread.view.webview.NestedScrollWebView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked();

    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mIsBeingDragged = false;//from w  w w  .j a  v a 2s. c  o  m

        // Remember where the motion event started
        mLastMotionY = (int) ev.getY();
        //                downY = (int) ev.getY();

        mActivePointerId = ev.getPointerId(0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;

    case MotionEvent.ACTION_MOVE:
        //                KLog.e("");
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            break;
        }
        //                if( !onlyVerticalMove(ev) ){
        //                    break;
        //                }

        final int y = (int) ev.getY(activePointerIndex);
        int deltaY = mLastMotionY - y;
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
        }
        // Scroll to follow the motion event
        mLastMotionY = y - mScrollOffset[1];

        final int oldY = getScrollY();
        final int scrolledDeltaY = Math.max(0, oldY + deltaY) - oldY;
        final int unconsumedY = deltaY - scrolledDeltaY;
        if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
            mLastMotionY -= mScrollOffset[1];
        }
        //                KLog.e("?");
        break;
    case MotionEvent.ACTION_UP:
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEvent.ACTION_CANCEL:
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        mLastMotionY = (int) ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) ev.getY(ev.findPointerIndex(mActivePointerId));
        break;
    default:
        break;
    }
    return super.onTouchEvent(ev);
}

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  . j a  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.commonsware.cwac.crossport.design.widget.HeaderBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }//from  w ww . j ava  2 s.  c o m

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        if (parent.isPointInChildBounds(child, x, y) && canDragView(child)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        } else {
            return false;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            return false;
        }

        final int y = (int) ev.getY(activePointerIndex);
        int dy = mLastMotionY - y;

        if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
            mIsBeingDragged = true;
            if (dy > 0) {
                dy -= mTouchSlop;
            } else {
                dy += mTouchSlop;
            }
        }

        if (mIsBeingDragged) {
            mLastMotionY = y;
            // We're being dragged so scroll the ABL
            scroll(parent, child, dy, getMaxDragOffset(child), 0);
        }
        break;
    }

    case MotionEvent.ACTION_UP:
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(ev);
            mVelocityTracker.computeCurrentVelocity(1000);
            float yvel = mVelocityTracker.getYVelocity(mActivePointerId);
            fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);
        }
        // $FALLTHROUGH
    case MotionEvent.ACTION_CANCEL: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

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

    return true;
}

From source file:com.leeon.blank.widget.RangeBarNew.java

private void handleTouchMove(MotionEvent event) {
    if (mLeftHit && mLeftPointerID != -1) {
        final int index = event.findPointerIndex(mLeftPointerID);
        final float x = event.getX(index);
        float deltaX = x - mLeftPointerLastX;
        mLeftPointerLastX = (int) x;
        DIRECTION direction = (deltaX < 0 ? DIRECTION.LEFT : DIRECTION.RIGHT);
        if (direction == DIRECTION.LEFT && mLeftCursorIndex == 0) {
            return;
        }//from w  w  w  .  j  a v a 2 s  .  co m
        if (mLeftCursorRect.left + deltaX < mLeftBoundary) {
            deltaX = mLeftBoundary - mLeftCursorRect.left;
        }
        if (isInfinite && (mLeftCursorRect.right + deltaX > mRightBoundary - mPartLength - slopDistance)) {
            deltaX = (float) (mRightBoundary - mPartLength - slopDistance - mLeftCursorRect.right);
        }
        // Check whether left and right cursor will collision.
        if (mLeftCursorRect.right + deltaX >= mRightCursorRect.left) {
            // Check whether right cursor is in "Touch" mode( if in touch
            // mode, represent that we can not move it at all), or right
            // cursor reach the boundary.
            if (mRightHit || Math.round(mRightCursorIndex) == (mTextArray.length - 1) * 5
                    || mRightScroller.computeScrollOffset()) {
                // Just move left cursor to the left side of right one.
                deltaX = mRightCursorRect.left - mLeftCursorRect.right;
            } else {
                // Move right cursor to higher location.
                mRightCursorNextIndex = Math.min(mRightCursorIndex + 1, (mTextArray.length - 1) * 5);
                if (!mRightScroller.computeScrollOffset()) {
                    final int fromX = (int) (mRightCursorIndex * mTinyPartLength);
                    mRightScroller.startScroll(fromX, 0,
                            (int) (mRightCursorNextIndex * mPartLength / 5 - fromX), 0, mDuration);
                }
            }
        }
        // After some calculate, if deltaX is still be zero, do quick return.
        if (deltaX == 0) {
            return;
        }
        // Calculate the movement.
        final float moveX = deltaX / mTinyPartLength;
        mLeftCursorIndex += moveX;
        invalidate();
    }
    if (mRightHit && mRightPointerID != -1) {
        final int index = event.findPointerIndex(mRightPointerID);
        final float x = event.getX(index);
        float deltaX = x - mRightPointerLastX;
        mRightPointerLastX = (int) x;
        DIRECTION direction = (deltaX < 0 ? DIRECTION.LEFT : DIRECTION.RIGHT);
        final int maxIndex = mTextArray.length - 1;
        if (direction == DIRECTION.RIGHT && mRightCursorIndex == maxIndex) {
            return;
        }
        if (mRightCursorRect.right + deltaX > mRightBoundary + slopDistance) {
            deltaX = (float) (mRightBoundary + slopDistance - mRightCursorRect.right);
        }
        if (mRightCursorRect.left + deltaX < mLeftCursorRect.right) {
            if (mLeftHit || Math.round(mLeftCursorIndex) == 0 || mLeftScroller.computeScrollOffset()) {
                deltaX = mLeftCursorRect.right - mRightCursorRect.left;
            } else {
                mLeftCursorNextIndex = Math.max(0, mLeftCursorIndex - 1);
                if (!mLeftScroller.computeScrollOffset()) {
                    final int fromX = (int) (mLeftCursorIndex * mPartLength / 5);
                    mLeftScroller.startScroll(fromX, 0, (int) (mLeftCursorNextIndex * mPartLength / 5 - fromX),
                            0, mDuration);
                }
            }
        }
        if (deltaX == 0) {
            return;
        }
        final float moveX = deltaX / mTinyPartLength;
        mRightCursorIndex += moveX;
        invalidate();
    }
}