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: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;//w  ww .ja v  a 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:android.support.design.widget.HeaderBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }//from ww  w.j  a  v a2s  .c om

    switch (MotionEventCompat.getActionMasked(ev)) {
    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 = VelocityTrackerCompat.getYVelocity(mVelocityTracker, 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.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;//w w w . j  a  v a2  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();
        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:android.support.v7.widget.ForwardingListener.java

/**
 * Observes motion events and determines when to start forwarding.
 *
 * @param srcEvent motion event in source view coordinates
 * @return true to start forwarding motion events, false otherwise
 *///from w ww  .  ja  v  a 2s .c  o m
private boolean onTouchObserved(MotionEvent srcEvent) {
    final View src = mSrc;
    if (!src.isEnabled()) {
        return false;
    }

    final int actionMasked = MotionEventCompat.getActionMasked(srcEvent);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = srcEvent.getPointerId(0);

        if (mDisallowIntercept == null) {
            mDisallowIntercept = new DisallowIntercept();
        }
        src.postDelayed(mDisallowIntercept, mTapTimeout);

        if (mTriggerLongPress == null) {
            mTriggerLongPress = new TriggerLongPress();
        }
        src.postDelayed(mTriggerLongPress, mLongPressTimeout);
        break;
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
        if (activePointerIndex >= 0) {
            final float x = srcEvent.getX(activePointerIndex);
            final float y = srcEvent.getY(activePointerIndex);

            // Has the pointer moved outside of the view?
            if (!pointInView(src, x, y, mScaledTouchSlop)) {
                clearCallbacks();

                // Don't let the parent intercept our events.
                src.getParent().requestDisallowInterceptTouchEvent(true);
                return true;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        clearCallbacks();
        break;
    }

    return false;
}

From source file:com.zac4j.widget.CircleIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from w  w w  .  j a va 2 s . c om*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        final float x = ev.getX(activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = ev.getX(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        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);
        }
        mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId));
        break;
    }

    return true;
}

From source file:link.fls.swipestack.SwipeHelper.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    try {//w w w . j  a  v  a  2 s.co m

        if (gestureDetector.onTouchEvent(event)) {
            // we want to catch the use-cases in the gesture detector
            return true;
        } else {
            // otherwise do you dragging animation job
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!mListenForTouchEvents || !mSwipeStack.isEnabled()) {
                    return false;
                }

                v.getParent().requestDisallowInterceptTouchEvent(true);
                mSwipeStack.onSwipeStart();
                mPointerId = event.getPointerId(0);
                mDownX = event.getX(mPointerId);
                mDownY = event.getY(mPointerId);

                return true;

            case MotionEvent.ACTION_MOVE:
                int pointerIndex = event.findPointerIndex(mPointerId);
                if (pointerIndex < 0)
                    return false;

                float dx = event.getX(pointerIndex) - mDownX;
                float dy = event.getY(pointerIndex) - mDownY;

                float newX = mObservedView.getX() + dx;
                float newY = mObservedView.getY() + dy;

                mObservedView.setX(newX);
                mObservedView.setY(newY);

                float dragDistanceX = newX - mInitialX;
                float swipeProgress = Math.min(Math.max(dragDistanceX / mSwipeStack.getWidth(), -1), 1);

                mSwipeStack.onSwipeProgress(swipeProgress);

                if (mRotateDegrees > 0) {
                    float rotation = mRotateDegrees * swipeProgress;
                    mObservedView.setRotation(rotation);
                }

                if (mOpacityEnd < 1f) {
                    float alpha = 1 - Math.min(Math.abs(swipeProgress * 2), 1);
                    mObservedView.setAlpha(alpha);
                }

                return true;

            case MotionEvent.ACTION_UP:

                v.getParent().requestDisallowInterceptTouchEvent(false);
                mSwipeStack.onSwipeEnd();
                checkViewPosition();

                return true;
            }
        }
    } catch (IllegalArgumentException iae) {
        //The same bug
        //https://github.com/chrisbanes/PhotoView/blob/92a2a281134ceddc6e402ba4a83cc91180db8115/sample/src/uk/co/senab/photoview/sample/HackyViewPager.java
        return false;
    }

    return false;
}

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

private void trackTouchEvent(MotionEvent event) {
    final int pointerIndex = event.findPointerIndex(mActivePointerId);
    final float x = event.getX(pointerIndex);

    if (Thumb.MIN.equals(pressedThumb) && !mSingleThumb) {
        setNormalizedMinValue(screenToNormalized(x + mThumbHalfWidth));
    } else if (Thumb.MAX.equals(pressedThumb)) {
        double normalized = screenToNormalized(
                x - mThumbHalfWidth + (mSingleThumb ? (1 - x / ((float) getWidth())) * padding : 0));
        setNormalizedMaxValue(normalized);
    }/*from  w  w w  .j  a  va 2s.co  m*/
}

From source file:com.linkbubble.ui.BubbleFlowView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {

    final int action = event.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && mInterceptingTouch && mTouchInterceptor != null) {
        return true;
    }//from ww w.  jav  a  2  s . c om

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE:
        if (mActiveTouchPointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = event.findPointerIndex(mActiveTouchPointerId);
        if (pointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActiveTouchPointerId + " in onInterceptTouchEvent");
            break;
        }

        final int y = (int) event.getY(pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > 0) {
            mLastMotionY = y;
            // Here is the crux of it all...
            if (mTouchInterceptor != null) {
                mInterceptingTouch = true;
            }
        }

    case MotionEvent.ACTION_DOWN:
        // ACTION_DOWN always refers to pointer index 0.
        mLastMotionY = (int) event.getY();
        mActiveTouchPointerId = event.getPointerId(0);
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mInterceptingTouch && mLongPress) {
            final float bubblePeriod = (float) Constant.BUBBLE_FLOW_ANIM_TIME / 1000.f;
            final float contentPeriod = bubblePeriod * 0.666667f; // 0.66667 is the normalized t value when f = 1.0f for overshoot interpolator of 0.5 tension
            MainController.get().expandBubbleFlow((long) (contentPeriod * 1000), false);
        }
        mActiveTouchPointerId = INVALID_POINTER;
        mInterceptingTouch = false;
        break;
    }

    if (super.onInterceptTouchEvent(event)) {
        return true;
    }

    return mInterceptingTouch;
}

From source file:com.lovejjfg.powerrefresh.PowerRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*from www  .  jav  a  2  s . c  om*/

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

    if (!isEnabled() || isRefreshing || isLoading || !canChildScrollUp() || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragged = false;

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

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }

        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        final float y = ev.getY(pointerIndex);
        startDragging(y);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
}

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

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

    final int action = ev.getAction();

    // Shortcut since we're being dragged
    if (action == MotionEvent.ACTION_MOVE && mIsBeingDragged) {
        return true;
    }

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        mIsBeingDragged = false;
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (canDragView(child) && parent.isPointInChildBounds(child, x, y)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            break;
        }

        final int y = (int) ev.getY(pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

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

    return mIsBeingDragged;
}