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

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//from w w  w.ja  v  a  2 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:
        mInitialY = (int) event.getY();
        int initialX = (int) event.getX();
        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:xyz.zpayh.hdimage.HDImageView.java

private boolean up(MotionEvent event, int touchCount) {

    if (mMaxTouchCount > 0 && (mIsZooming || mIsPanning)) {
        if (mIsZooming && touchCount == 2) {
            mIsPanning = true;//from w  w w.  jav a  2 s  .  c  om
            mViewTranslateStart.set(mViewTranslate.x, mViewTranslate.y);
            if (event.getActionIndex() == 1) {
                mLastViewCenter.set(event.getX(0), event.getY(0));
            } else {
                mLastViewCenter.set(event.getX(1), event.getY(1));
            }
        }
        if (touchCount < 3) {
            mIsZooming = false;
        }
        if (touchCount < 2) {
            mIsPanning = false;
            mMaxTouchCount = 0;
        }

        refreshRequiredTiles(true);
        return true;
    }
    if (touchCount == 1) {
        mIsZooming = false;
        mIsPanning = false;
        mMaxTouchCount = 0;
    }
    return false;
}

From source file:com.baitouwei.swiperefresh.ASwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isEnabled() || (canChildScrollUp() && canChildScrollDown())
            || footerView.getSwipeStatus() == SwipeStatus.REFRESHING
            || headerView.getSwipeStatus() == SwipeStatus.REFRESHING
            || (swipeDownRefreshListener == null && swipeUpRefreshListener == null) || isAnimating()) {
        return false;
    }/*from w w  w . j a va 2  s  .com*/

    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        isDragging = false;

        //reset
        currentContentOffset = 0;
        headerView.setCurrentOffset(0);
        footerView.setCurrentOffset(0);
        headerView.updateStatus(SwipeStatus.NORMAL);
        footerView.updateStatus(SwipeStatus.NORMAL);

        activePointerId = MotionEventCompat.getPointerId(ev, ev.getActionIndex());
        initialMotionY = getMotionEventY(ev, activePointerId);
        break;
    case MotionEvent.ACTION_MOVE:
        if (activePointerId == INVALID_POINTER) {
            return false;
        }

        final float diffY = getMotionEventY(ev, activePointerId) - initialMotionY;

        //deal border
        if ((diffY > 0 && !canChildScrollUp() || (diffY < 0 && !canChildScrollDown()))) {
            if (Math.abs(diffY) > touchSlop && !isDragging) {
                isDragging = true;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        isDragging = false;
        activePointerId = INVALID_POINTER;
        break;
    }
    return isDragging;
}

From source file:com.ruesga.timelinechart.TimelineChartView.java

/** {@inheritDoc} */
@Override/*w w w  .  java 2  s  .c  om*/
public boolean onTouchEvent(final MotionEvent event) {
    // Ignore events while performing scrolling animation
    if (mState == STATE_ZOOMING) {
        return true;
    }

    final int action = event.getActionMasked();
    final int index = event.getActionIndex();
    final int pointerId = event.getPointerId(index);
    final long now = System.currentTimeMillis();
    mLastX = event.getX();
    mLastY = event.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Initialize velocity tracker
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {
            mVelocityTracker.clear();
        }
        mVelocityTracker.addMovement(event);
        mScroller.forceFinished(true);
        releaseEdgeEffects();
        mState = STATE_INITIALIZE;

        mLongPressDetector.mLongPressTriggered = false;
        mUiHandler.postDelayed(mLongPressDetector, mLongPressTimeout);

        mInitialTouchOffset = mCurrentOffset;
        mInitialTouchX = event.getX();
        mInitialTouchY = event.getY();
        mLastPressTimestamp = now;
        return true;

    case MotionEvent.ACTION_MOVE:
        // If a long press was detected then we end with the movement
        if (mLongPressDetector.mLongPressTriggered) {
            return true;
        }

        mVelocityTracker.addMovement(event);
        float diffX = event.getX() - mInitialTouchX;
        float diffY = event.getY() - mInitialTouchY;
        if (Math.abs(diffX) > mTouchSlop || mState >= STATE_MOVING) {
            mUiHandler.removeCallbacks(mLongPressDetector);

            mCurrentOffset = mInitialTouchOffset + diffX;
            if (mCurrentOffset < 0) {
                onOverScroll();
                mCurrentOffset = 0;
            } else if (mCurrentOffset > mMaxOffset) {
                onOverScroll();
                mCurrentOffset = mMaxOffset;
            }
            mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
            mState = STATE_MOVING;
            ViewCompat.postInvalidateOnAnimation(this);
        } else if (Math.abs(diffY) > mTouchSlop && mState < STATE_MOVING) {
            mUiHandler.removeCallbacks(mLongPressDetector);
            return false;
        }
        return true;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mUiHandler.removeCallbacks(mLongPressDetector);
        // If a long press was detected then we end with the movement
        if (mLongPressDetector.mLongPressTriggered) {
            return true;
        }

        if (mState >= STATE_MOVING) {
            final int velocity = (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
            mScroller.forceFinished(true);
            mState = STATE_FLINGING;
            releaseEdgeEffects();
            mScroller.fling((int) mCurrentOffset, 0, velocity, 0, 0, (int) mMaxOffset, 0, 0);
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            // Reset scrolling state
            mState = STATE_IDLE;

            if (action == MotionEvent.ACTION_UP) {
                // we are in a tap or long press action
                final long timeDiff = (now - mLastPressTimestamp);
                // If diff < 0, that means that time have change. ignore this event
                if (timeDiff >= 0) {
                    if (timeDiff > TAP_TIMEOUT && timeDiff < mLongPressTimeout) {
                        // A tap event happens. Long click are detected outside
                        Message.obtain(mUiHandler, MSG_ON_CLICK_ITEM, computeItemEvent()).sendToTarget();
                    }
                }
            }
        }

        mLastPressTimestamp = -1;
        return true;
    }
    return false;
}

From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_CANCEL:
        Log.d(TAG, "Cancel Event");
        break;/* w  w  w .j  ava  2s  .  c  o  m*/
    case MotionEvent.ACTION_DOWN:
        Log.d(TAG, "Press Event");
        break;
    case MotionEvent.ACTION_UP:
        Log.d(TAG, "Up Event");

        if (mVelocityTracker == null) {
            Log.d(TAG, "velocity tracker is null in action up");
            break;
        }

        Log.d(TAG, "Up Intercept");
        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        mVelocityTracker.addMovement(e);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (absDeltaX > mViewWidth / 2) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss) {
            dismiss(mChildView, mChildPosition, dismissRight);
        } else {
            mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null);
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    case MotionEvent.ACTION_MOVE:
        Log.d(TAG, "Move Event");
        mRecyclerView.requestDisallowInterceptTouchEvent(true);
        mRefreshLayout.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(e);
        cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL | (e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mRecyclerView.onTouchEvent(cancelEvent);
        mRefreshLayout.onTouchEvent(cancelEvent);
        cancelEvent.recycle();

        mChildView.setTranslationX(mDeltaX);
        /*mChildView.setAlpha(Math.max(0.15f, Math.min(1f,
                                                     1f - 2f * Math.abs(mDeltaX) /
                                                          mViewWidth)));*/
        break;
    }
}

From source file:com.android.internal.widget.ViewPager.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = ev.getActionIndex();
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = ev.getX(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();/* w  w w . j  a  va  2 s .co  m*/
        }
    }
}

From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java

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

    if (canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return super.onTouchEvent(ev);
    }//from ww w  .  j  a  v a2  s.c  o m

    mVelocityTracker.addMovement(ev);

    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialTouchX = x;
        mInitialTouchY = mLastTouchY = y;
        mActivePointerId = ev.getPointerId(0);
        if (findChildUnder(mInitialTouchX, mInitialTouchY) == null && mClickOutsideListener != null) {
            mIsDragging = handled = true;
        }
        handled |= mCollapsibleHeight > 0;
        mScroller.abortAnimation();
    }
        break;

    case MotionEvent.ACTION_MOVE: {
        int index = ev.findPointerIndex(mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "Bad pointer id " + mActivePointerId + ", resetting");
            index = 0;
            mActivePointerId = ev.getPointerId(0);
            mInitialTouchX = ev.getX();
            mInitialTouchY = mLastTouchY = ev.getY();
        }
        final float x = ev.getX(index);
        final float y = ev.getY(index);
        if (!mIsDragging) {
            final float dy = y - mInitialTouchY;
            if (Math.abs(dy) > mTouchSlop && findChildUnder(x, y) != null) {
                handled = mIsDragging = true;
                mLastTouchY = Math.max(mLastTouchY - mTouchSlop,
                        Math.min(mLastTouchY + dy, mLastTouchY + mTouchSlop));
            }
        }
        if (mIsDragging) {
            final float dy = y - mLastTouchY;
            performDrag(dy);
        }
        mLastTouchY = y;
    }
        break;

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerIndex = ev.getActionIndex();
        final int pointerId = ev.getPointerId(pointerIndex);
        mActivePointerId = pointerId;
        mInitialTouchX = ev.getX(pointerIndex);
        mInitialTouchY = mLastTouchY = ev.getY(pointerIndex);
    }
        break;

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
    }
        break;

    case MotionEvent.ACTION_UP: {
        mIsDragging = false;
        if (!mIsDragging && findChildUnder(mInitialTouchX, mInitialTouchY) == null
                && findChildUnder(ev.getX(), ev.getY()) == null) {
            if (mClickOutsideListener != null) {
                mClickOutsideListener.onClick(this);
                resetTouch();
                return true;
            }
        }
        if (mOpenOnClick && Math.abs(ev.getX() - mInitialTouchX) < mTouchSlop
                && Math.abs(ev.getY() - mInitialTouchY) < mTouchSlop) {
            smoothScrollTo(0, 0);
            return true;
        }
        mVelocityTracker.computeCurrentVelocity(1000);
        final float yvel = mVelocityTracker.getYVelocity(mActivePointerId);
        if (Math.abs(yvel) > mMinFlingVelocity) {
            smoothScrollTo(yvel < 0 ? 0 : mCollapsibleHeight, yvel);
        } else {
            smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0);
        }
        resetTouch();
    }
        break;

    case MotionEvent.ACTION_CANCEL: {
        if (mIsDragging) {
            smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0);
        }
        resetTouch();
        return true;
    }
    }

    return handled || (action == MotionEvent.ACTION_MOVE && mCollapseOffset > 0);
}

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private void onQsTouch(MotionEvent event) {
    int pointerIndex = event.findPointerIndex(mTrackingPointer);
    if (pointerIndex < 0) {
        pointerIndex = 0;/* ww  w .j  a v  a  2  s . c  om*/
        mTrackingPointer = event.getPointerId(pointerIndex);
    }
    final float y = event.getY(pointerIndex);
    final float x = event.getX(pointerIndex);
    final float h = y - mInitialTouchY;
    logf("onQsTouch() touch event = " + event.getActionMasked());
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        logf("onQsTouch() touch event = MotionEvent.ACTION_DOWN ");
        mQsTracking = true;
        mInitialTouchY = y;
        mInitialTouchX = x;
        onQsExpansionStarted();
        mInitialHeightOnTouch = mQsExpansionHeight;
        initVelocityTracker();
        trackMovement(event);
        break;

    case MotionEvent.ACTION_POINTER_UP:
        logf("onQsTouch() touch event = MotionEvent.ACTION_POINTER_UP ");
        final int upPointer = event.getPointerId(event.getActionIndex());
        if (mTrackingPointer == upPointer) {
            // gesture is ongoing, find a new pointer to track
            final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
            final float newY = event.getY(newIndex);
            final float newX = event.getX(newIndex);
            mTrackingPointer = event.getPointerId(newIndex);
            mInitialHeightOnTouch = mQsExpansionHeight;
            mInitialTouchY = newY;
            mInitialTouchX = newX;
        }
        break;

    case MotionEvent.ACTION_MOVE:
        logf("onQsTouch() touch event = MotionEvent.ACTION_MOVE ");
        setQsExpansion(h + mInitialHeightOnTouch);
        if (h >= getFalsingThreshold()) {
            mQsTouchAboveFalsingThreshold = true;
        }
        trackMovement(event);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        logf("onQsTouch() touch event = MotionEvent.ACTION_UP/ACTION_CANCEL");
        mQsTracking = false;
        mTrackingPointer = -1;
        trackMovement(event);
        float fraction = getQsExpansionFraction();
        if ((fraction != 0f || y >= mInitialTouchY) && (fraction != 1f || y <= mInitialTouchY)) {
            flingQsWithCurrentVelocity(y, event.getActionMasked() == MotionEvent.ACTION_CANCEL);
        } else {
            logQsSwipeDown(y);
            mScrollYOverride = -1;
        }
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
}

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private boolean onQsIntercept(MotionEvent event) {
    int pointerIndex = event.findPointerIndex(mTrackingPointer);
    if (pointerIndex < 0) {
        pointerIndex = 0;//from   w w  w  . j a  v a  2 s .c om
        mTrackingPointer = event.getPointerId(pointerIndex);
    }
    final float x = event.getX(pointerIndex);
    final float y = event.getY(pointerIndex);

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mIntercepting = true;
        mInitialTouchY = y;
        mInitialTouchX = x;
        initVelocityTracker();
        trackMovement(event);
        if (shouldQuickSettingsIntercept(mInitialTouchX, mInitialTouchY, 0)) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        if (mQsExpansionAnimator != null) {
            onQsExpansionStarted();
            mInitialHeightOnTouch = mQsExpansionHeight;
            mQsTracking = true;
            mIntercepting = false;
            mNotificationStackScroller.removeLongPressCallback();
        }
        break;
    case MotionEvent.ACTION_POINTER_UP:
        final int upPointer = event.getPointerId(event.getActionIndex());
        if (mTrackingPointer == upPointer) {
            // gesture is ongoing, find a new pointer to track
            final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
            mTrackingPointer = event.getPointerId(newIndex);
            mInitialTouchX = event.getX(newIndex);
            mInitialTouchY = event.getY(newIndex);
        }
        break;

    case MotionEvent.ACTION_MOVE:
        final float h = y - mInitialTouchY;
        trackMovement(event);
        if (mQsTracking) {

            // Already tracking because onOverscrolled was called. We need to update here
            // so we don't stop for a frame until the next touch event gets handled in
            // onTouchEvent.

            setQsExpansion(h + mInitialHeightOnTouch);
            trackMovement(event);
            mIntercepting = false;
            return true;
        }
        if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)
                && shouldQuickSettingsIntercept(mInitialTouchX, mInitialTouchY, h)) {
            mQsTracking = true;
            onQsExpansionStarted();
            notifyExpandingFinished();
            mInitialHeightOnTouch = mQsExpansionHeight;
            mInitialTouchY = y;
            mInitialTouchX = x;
            mIntercepting = false;
            mNotificationStackScroller.removeLongPressCallback();
            return true;
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        trackMovement(event);
        if (mQsTracking) {
            flingQsWithCurrentVelocity(y, event.getActionMasked() == MotionEvent.ACTION_CANCEL);
            mQsTracking = false;
        }
        mIntercepting = false;
        break;
    }
    return false;
}