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:com.android.incallui.widget.multiwaveview.GlowPadView.java

private void handleUp(MotionEvent event) {
    if (DEBUG && mDragging)
        Log.v(TAG, "** Handle RELEASE");
    int actionIndex = event.getActionIndex();
    if (event.getPointerId(actionIndex) == mPointerId) {
        switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex));
    }/*from   www  .  j a v a 2 s  .  c  o m*/
}

From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java

private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = ev.getPointerCount();
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = ev.getPointerId(i);
        final float x = ev.getX(i);
        final float y = ev.getY(i);
        mLastMotionX[pointerId] = x;//from   w  ww .j  av  a  2 s .  co m
        mLastMotionY[pointerId] = y;
    }
}

From source file:com.gu.swiperefresh.SwipeRefreshPlush.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = MotionEventCompat.getActionMasked(event);
    int pointerIndex;
    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;//from   w  w  w .j  a v  a2  s.  c  o m
    }

    if (!isEnabled() || mReturningToStart || mRefreshController.isRefresh() || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mIsBeingDragUp = false;
        mIsBeingDragDown = false;
        mActivePointerId = event.getPointerId(0);
        pointerIndex = event.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        mInitialDownY = event.getY(pointerIndex);
        mLastY = mInitialDownY;
        return false;
    case MotionEvent.ACTION_CANCEL:
        return false;
    case MotionEvent.ACTION_MOVE: {
        pointerIndex = event.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            // Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = event.getY(pointerIndex);
        startDragging(y);

        if (mIsBeingDragUp) {
            final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            if (overscrollTop > 0) {
                mRefreshController.showPullRefresh(overscrollTop);
            }

        } else if (mIsBeingDragDown) {
            int dy = (int) (y - mLastY);
            Log.i(TAG, "lasty:" + mLastY);
            Log.i(TAG, "dy:" + dy);
            //
            if (dy >= 0.5) {
                hideLoadMoreView(Math.abs(dy));
            } else if (dy < -0.5) {
                showLoadMoreView(Math.abs(dy));
            }
        }
        mLastY = y;
        break;
    }
    case MotionEvent.ACTION_UP: {
        pointerIndex = event.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            //  Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            return false;
        }

        if (mIsBeingDragUp) {
            final float y = event.getY(pointerIndex);
            final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            mIsBeingDragUp = false;
            if (overscrollTop > 0)
                mRefreshController.finishPullRefresh(overscrollTop);
        }
        if (mIsBeingDragDown) {
            final float y = event.getY(pointerIndex);
            final float overscrollBottom = (y - mInitialMotionY);
            mIsBeingDragDown = false;
            if (overscrollBottom < 0)
                mLoadViewController.finishPullRefresh(Math.abs(overscrollBottom));
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }
    return true;
}

From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java

/**
 * Called by the framework when your view for creating input needs to
 * be generated.  This will be called the first time your input method
 * is displayed, and every time it needs to be re-created such as due to
 * a configuration change./*from   w w  w  . j a va 2 s  .com*/
 */
@Override
public View onCreateInputView() {
    mInputView = (KbLatinKeyboardView) getLayoutInflater().inflate(R.layout.input, null);
    mInputView.setKeyboard(mQwertyKeyboard);
    mInputView.setOnKeyboardActionListener(this);

    /****************************************************edit 2**************************************************************/

    featuredb = new DataDBHelper(this);
    KbSoftKeyboard gd = new KbSoftKeyboard();
    sf = new Save_feature();
    swipeWord = new GetSwipeWord(this);
    //gDetect.setOnDoubleTapListener(gd);
    mInputView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int index = event.getActionIndex();
            int action = event.getActionMasked();
            int pointerId = event.getPointerId(index);

            //check for actions of motion event
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //retrieve key at current
                char ch = getkeylabel(event);
                if (ch > 0)
                    swipe += ch;
                // test+=swipe.charAt(0);
                //set up start timer for measuring duration
                //  start=System.currentTimeMillis();
                //setup velocity tracker
                if (mvel == null) {
                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                    mvel = VelocityTracker.obtain();
                } else {
                    // Reset the velocity tracker back to its initial state.
                    mvel.clear();
                }
            }

            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                mvel.addMovement(event);
                mvel.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                x_vel += abs(VelocityTrackerCompat.getXVelocity(mvel, pointerId));
                y_vel += abs(VelocityTrackerCompat.getYVelocity(mvel, pointerId));
                n_event += 1;
                //  Log.d("", "X velocity: " +  x_vel);
                //  Log.d("", "Y velocity: " +  y_vel);

            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                //record time when finger lifted up
                //           end=System.currentTimeMillis();
                //calculate duration
                //         duration=(end-start)/100;
                //calculate velocity pixels per sec
                //  Log.d("", "X velocity: " +  x_vel);
                // Log.d("", "Y velocity: " +  y_vel);

                velocity = Math.sqrt(x_vel * x_vel + y_vel * y_vel);
                //obtain pressure
                pressure += event.getPressure();
                np_event += 1;
                swipe2 = get_final_string(swipe);
                swipe = "";
                if (swipe2 == null)
                    swipe2 = "";
                //print generated string
                System.out.println(swipe + "\n 2nd " + swipe2);
            }

            if (((int) old_x) == 0 & ((int) old_y) == 0) {
                old_x = event.getX();
                old_y = event.getY();
                swipe += getkeylabel(event);
            } else if (((int) olddir_x) == 0 & ((int) olddir_y) == 0) {
                olddir_x = event.getX() - old_x;
                olddir_y = event.getY() - old_y;
                old_x = event.getX();
                old_y = event.getY();
            } else {
                check_change(event);
            }

            // Return false to avoid consuming the touch event
            return false;
        }
    });

    //***********************************************edit 2 end*****************************************************************

    //Saved my life
    if (mInputView.isPreviewEnabled())
        mInputView.setPreviewEnabled(false);
    return mInputView;
}

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

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//www  .j a  v  a 2s  . 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) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return true;
}

From source file:org.florescu.android.rangeseekbar.RangeSeekBar.java

/**
 * Handles thumb selection and movement. Notifies listener callback on certain events.
 */// ww w .  ja  va2 s. co  m
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    if (!isEnabled()) {
        return false;
    }

    int pointerIndex;

    final int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:
        // Remember where the motion event started
        mActivePointerId = event.getPointerId(event.getPointerCount() - 1);
        pointerIndex = event.findPointerIndex(mActivePointerId);
        mDownMotionX = event.getX(pointerIndex);

        pressedThumb = evalPressedThumb(mDownMotionX);

        // Only handle thumb presses.
        if (pressedThumb == null) {
            pressedThumb = getClosestThumb(mDownMotionX);
            if (pressedThumb == null) {
                return false;
            }
        }

        if (listener != null) {
            listener.onRangeSeekBarPressed(this,
                    Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                    pressedThumb);
        }

        setPressed(true);
        invalidate();
        onStartTrackingTouch();
        trackTouchEvent(event);
        attemptClaimDrag();

        break;
    case MotionEvent.ACTION_MOVE:
        if (pressedThumb != null) {

            if (mIsDragging) {
                trackTouchEvent(event);
            } else {
                // Scroll to follow the motion event
                pointerIndex = event.findPointerIndex(mActivePointerId);
                final float x = event.getX(pointerIndex);

                if (Math.abs(x - mDownMotionX) > mScaledTouchSlop) {
                    setPressed(true);
                    invalidate();
                    onStartTrackingTouch();
                    trackTouchEvent(event);
                    attemptClaimDrag();
                }
            }

            if (notifyWhileDragging && listener != null) {
                listener.onRangeSeekBarValueChanged(this,
                        Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                        pressedThumb);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsDragging) {
            trackTouchEvent(event);
            onStopTrackingTouch();
            setPressed(false);
        } else {
            // Touch up when we never crossed the touch slop threshold
            // should be interpreted as a tap-seek to that location.
            onStartTrackingTouch();
            trackTouchEvent(event);
            onStopTrackingTouch();
        }

        if (listener != null) {
            listener.onRangeSeekBarValueChanged(this,
                    Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                    pressedThumb);
        }

        pressedThumb = null;
        invalidate();

        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = event.getPointerCount() - 1;
        // final int index = ev.getActionIndex();
        mDownMotionX = event.getX(index);
        mActivePointerId = event.getPointerId(index);
        invalidate();
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(event);
        invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
            onStopTrackingTouch();
            setPressed(false);
        }
        invalidate(); // see above explanation
        break;
    }
    return true;
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = ev.getX(newPointerIndex);
        mLastMotionX2 = ev.getX(newPointerIndex);
        mLastMotionY = ev.getY(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();//w  w  w  .  ja  v a2  s . co  m
        }
    }
}

From source file:org.alex.refreshlayout.RefreshLayout.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }//from  w w w.jav  a2s  .c  o  m
}

From source file:com.gu.swiperefresh.SwipeRefreshPlush.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*from  w ww.  j  ava 2  s . c o  m*/

    final int action = MotionEventCompat.getActionMasked(ev);
    int pointerIndex;

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || (!canLoadMore() && !canRefresh()) || mReturningToStart || mRefreshController.isRefresh()
            || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mRefreshController.setTargetOffsetTopAndBottom(
                mRefreshController.getCurrentTargetOffsetTop() - mRefreshView.getTop(), true);
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragUp = false;
        mIsBeingDragDown = false;

        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        mInitialDownY = ev.getY(pointerIndex);
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }
        mVelocityTracker.addMovement(ev);
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        final float y = ev.getY(pointerIndex);
        startDragging(y);
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        float initialVelocity = velocityTracker.getYVelocity(mActivePointerId);
        Log.d(TAG, "fling:" + initialVelocity);
        if (Math.abs(initialVelocity) > mMinimumVelocity) {
            flingWithNestedDispatch(0, -initialVelocity);
        }
        releaseVelocityTracker();
        break;
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragUp = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragUp || mIsBeingDragDown;
}

From source file:com.android.mail.browse.ConversationContainer.java

/**
 * Touch slop code was copied from {@link ScrollView#onInterceptTouchEvent(MotionEvent)}.
 *//*from w  w w .  j  ava 2s.com*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mTouchInitialized) {
        mTouchInitialized = true;
    }

    // no interception when WebView handles the first DOWN
    if (mWebView.isHandlingTouch()) {
        return false;
    }

    boolean intercept = false;
    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_POINTER_DOWN:
        LogUtils.d(TAG, "Container is intercepting non-primary touch!");
        intercept = true;
        mMissedPointerDown = true;
        requestDisallowInterceptTouchEvent(true);
        break;

    case MotionEvent.ACTION_DOWN:
        mLastMotionY = ev.getY();
        mActivePointerId = ev.getPointerId(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
        final float y = ev.getY(pointerIndex);
        final int yDiff = (int) Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mLastMotionY = y;
            intercept = true;
        }
        break;
    }

    //        LogUtils.v(TAG, "in Container.InterceptTouch. action=%d x/y=%f/%f pointers=%d result=%s",
    //                ev.getActionMasked(), ev.getX(), ev.getY(), ev.getPointerCount(), intercept);
    return intercept;
}