Example usage for android.view MotionEvent getPointerId

List of usage examples for android.view MotionEvent getPointerId

Introduction

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

Prototype

public final int getPointerId(int pointerIndex) 

Source Link

Document

Return the pointer identifier associated with a particular pointer data index in this event.

Usage

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

private PointerInfo pointerInfoForEventIndex(MotionEvent event, int index) {
    int id = event.getPointerId(index);
    for (PointerInfo pointerInfo : mPointerInfo) {
        if (pointerInfo.getId() == id) {
            return pointerInfo;
        }/*ww w  . j av  a  2 s .c  o  m*/
    }
    return null;
}

From source file:info.papdt.blacklight.ui.common.DragRelativeLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mDraggable == null)
        return super.onTouchEvent(event);

    float x = event.getX();
    float y = event.getY();

    if (insideDraggable(x, y)) {
        mHelper.captureChildView(mDraggable, event.getPointerId(0));
    }// w w w.ja  v  a  2  s . co m

    try {
        mHelper.processTouchEvent(event);
    } catch (IllegalArgumentException e) {
        return false;
    }
    return true;
}

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

private boolean down(MotionEvent event) {
    activePointerId = event.getPointerId(0);
    downY = (int) event.getY();
    downX = (int) event.getX();

    return false;
}

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

private void onTouchEnd(MotionEvent event) {
    mLastEventTime = event.getEventTime();

    int id = event.getPointerId(getActionIndex(event));
    ListIterator<PointerInfo> iterator = mPointerInfo.listIterator();
    while (iterator.hasNext()) {
        PointerInfo pointerInfo = iterator.next();
        if (pointerInfo.getId() != id) {
            continue;
        }/*w ww  . jav  a2 s  . c  o m*/

        // One of the pointers we were tracking was lifted. Remove its info object from the
        // list, recycle it to avoid GC pauses, and send an onScaleEnd() notification if this
        // ended the gesture.
        iterator.remove();
        pointerInfo.recycle();
        if (getPointersDown() == 1) {
            sendScaleGesture(EventType.END);
        }
    }
}

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

/**
 * Compares if {@code event} on {@code touchedView} is a continuation of the event stream
 * tracked by this PendingDrag.  This includes whether the event stream has had sufficient
 * regular updates, at least more often than {@link #MAX_MOTION_EVENT_MILLISECONDS_DELTA}
 * (in an effort to disregard it from dropped previous streams with dropped
 * {@link MotionEvent#ACTION_UP} and {@link MotionEvent#ACTION_CANCEL}).  If that threshold
 * is exceeded (for matching view and pointer id), the PendingDrag will no longer be alive
 * ({@link #isAlive()}, and not match any future events.
 * <p/>/*ww w  . j  a  v a  2  s.c o  m*/
 * If the event is a match and alive, it will pass the event through a {@link GestureDetector}
 * to determine if the event triggers a click (or other interesting gestures in the future).
 * Check {@link #isClick()} to determine whether a click was detected.
 * <p/>
 * This method should only be called from {@link Dragger#onTouchBlockImpl}.
 *
 * @param event The event to compare to.
 * @param touchedView The view that received the touch event.
 * @return Whether the event was a match and the drag is still alive.
 */
boolean isMatchAndProcessed(MotionEvent event, BlockView touchedView) {
    if (!mAlive) {
        return false;
    }

    final int pointerId = event.getPointerId(event.getActionIndex());
    long curEventTime = event.getEventTime();
    long deltaMs = curEventTime - mLatestEventTime;
    if (deltaMs < MAX_MOTION_EVENT_MILLISECONDS_DELTA) {
        if (pointerId == mPointerId && touchedView == mTouchedView) {
            mLatestEventTime = curEventTime;
            mGestureDetector.onTouchEvent(event);
            return true;
        }
    } else {
        mAlive = false; // Exceeded threshold and expired.
    }

    return false; // Not a pointer & view match or died.
}

From source file:com.arthurpitman.common.SlidingPanel.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(event.getActionIndex());

    switch (action) {
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_DOWN:
        startTouchY = y;//from www. j a v  a2 s  .c om
        startTouchScrollY = getScrollY();

        // if touch didn't occur on the actual control, ignore it
        float touchBoundary = viewHeight - collapsedHeight - startTouchScrollY;
        if (y < touchBoundary) {
            return false;
        }

        // start tracking velocity
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);
        break;

    case MotionEvent.ACTION_MOVE:
        // determine if a valid touch has started
        if (Math.abs(y - startTouchY) > touchSlop) {
            touchState = TOUCH_STARTED;
        }
        if (velocityTracker != null) {
            velocityTracker.addMovement(event);
        }

        // scroll as appropriate
        if (touchState == TOUCH_STARTED) {
            final int scrollDelta = (int) (startTouchY - y);
            scrollTo(0, Math.max(0, Math.min(viewHeight, startTouchScrollY + scrollDelta)));
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (touchState == TOUCH_STARTED) {
            final int currentScrollY = getScrollY();

            // get velocity
            float velocity = 0;
            if (velocityTracker != null) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = VelocityTrackerCompat.getYVelocity(velocityTracker, pointerId);
                velocityTracker.recycle();
                velocityTracker = null;
            }

            // snap to final scroll position
            int target = startTouchScrollY;
            if ((Math.abs(velocity) > minimumflingVelocity) || (Math.abs(y - startTouchY) > (viewHeight / 2))) {
                if (velocity < 0) {
                    target = viewHeight;
                } else {
                    target = 0;
                }
            }
            scroller.startScroll(0, currentScrollY, 0, target - currentScrollY);
            invalidate();
            touchState = TOUCH_NONE;
        }
        break;
    }
    return true;
}

From source file:com.fishstix.dosboxfree.ButtonLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);//((action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT);
    final int pId = ev.getPointerId(pointerIndex) + 1;
    KeyEvent evt = null;/*from ww w . j  ava2  s.  c  o  m*/
    Message msg = Message.obtain();
    msg.what = DBMain.HANDLER_SEND_KEYCODE;
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN: {
        Log.i("DosBoxTurbo", "button onDown()");
        int x = (int) ev.getX(pointerIndex);//(int) mWrap.getX(ev,pointerIndex);
        int width = getWidth();
        //int y = (int) mWrap.getY(ev,pointerIndex);
        float val = ((float) x / (float) width) * 4f;
        if (val < 1.0) {
            // first button
            if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_A) > 0)
                return false;
            evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_A);
            virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_A);
            msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_A;
            mDBLauncher.bButtonA.setBackgroundColor(0x80FF0000);
        } else if (val < 2.0) {
            if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_B) > 0)
                return false;
            evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_B);
            virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_B);
            msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_B;
            mDBLauncher.bButtonB.setBackgroundColor(0x80FF0000);
        } else if (val < 3.0) {
            if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_C) > 0)
                return false;
            evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_C);
            virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_C);
            msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_C;
            mDBLauncher.bButtonC.setBackgroundColor(0x80FF0000);
        } else {
            if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_D) > 0)
                return false;
            evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_D);
            virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_D);
            msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_D;
            mDBLauncher.bButtonD.setBackgroundColor(0x80FF0000);
        }
        msg.obj = evt;
        msg.arg1 = 0;
        mDBLauncher.mSurfaceView.virtButton[pointerIndex] = true;
        mDBLauncher.mSurfaceView.mFilterLongClick = true; // prevent long click listener from getting in the way
        mDBLauncher.mHandler.sendMessage(msg);
        return true;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP: {
        Log.i("DosBoxTurbo", "button onUp()");
        msg.arg2 = virtualbutton_hm.get(pId);
        switch (msg.arg2) {
        case HardCodeWrapper.KEYCODE_VIRTUAL_A:
            mDBLauncher.bButtonA.setBackgroundColor(0x80FFFF00);
            break;
        case HardCodeWrapper.KEYCODE_VIRTUAL_B:
            mDBLauncher.bButtonB.setBackgroundColor(0x80FFFF00);
            break;
        case HardCodeWrapper.KEYCODE_VIRTUAL_C:
            mDBLauncher.bButtonC.setBackgroundColor(0x80FFFF00);
            break;
        case HardCodeWrapper.KEYCODE_VIRTUAL_D:
            mDBLauncher.bButtonD.setBackgroundColor(0x80FFFF00);
            break;
        }
        virtualbutton_hm.delete(pId);
        if (msg.arg2 == 0)
            return false;
        evt = new KeyEvent(action, msg.arg2);
        msg.obj = evt;
        msg.arg1 = 1;
        mDBLauncher.mSurfaceView.virtButton[pointerIndex] = false;
        mDBLauncher.mSurfaceView.mFilterLongClick = false;
        mDBLauncher.mHandler.sendMessage(msg);
        return true;
    }
    }
    return false;
}

From source file:com.anysoftkeyboard.devicespecific.AskV8GestureDetector.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {
    int singleFingerEventPointerId = mSingleFingerEventPointerId;

    //I want to keep track on the first finger (https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/300)
    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN:
        if (ev.getPointerCount() == 1) {
            mSingleFingerEventPointerId = ev.getPointerId(0);
            singleFingerEventPointerId = mSingleFingerEventPointerId;
        }/*  w w w  . ja v a 2s  . c  o  m*/
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (ev.getPointerCount() == 1)
            mSingleFingerEventPointerId = NOT_A_POINTER_ID;
    }
    try {
        //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/26
        mScaleGestureDetector.onTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        //I have nothing I can do here.
    } catch (ArrayIndexOutOfBoundsException e) {
        //I have nothing I can do here.
    }
    //I'm going to pass the event to the super, only if it is a single touch, and the event is for the first finger
    //https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/300
    if (ev.getPointerCount() == 1 && ev.getPointerId(0) == singleFingerEventPointerId)
        return super.onTouchEvent(ev);
    else
        return false;
}

From source file:org.deviceconnect.android.deviceplugin.wear.activity.WearTouchProfileActivity.java

/**
 * Send event data./*from   w w  w  .jav  a  2  s. co m*/
 *
 * @param action Action.
 * @param event MotionEvent.
 */
private void sendEventData(final String action, final MotionEvent event) {
    int dataCount = event.getPointerCount();
    StringBuilder data = new StringBuilder(String.valueOf(dataCount));
    data.append(",").append(action);
    for (int n = 0; n < dataCount; n++) {
        int pointerId = event.getPointerId(n);
        data.append(",").append(pointerId).append(",").append(event.getX(n)).append(",").append(event.getY(n));
    }

    sendEvent(WearConst.WEAR_TO_DEVICE_TOUCH_DATA, data.toString());
}

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  ww .j  a  v a2  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;
}