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.com.mytoolsproject.SwipeRefreshLayout.java

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

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

    //mTarget??
    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    //??mCircleView???mTarget?
    //?mNestedScrollInProgress
    // ?????mTarget
    if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing || mNestedScrollInProgress) {
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        //??
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = ev.getPointerId(0);
        //?
        mIsBeingDragged = false;

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

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        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:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();

    if (!isEnabled()) {
        return false; // We don't want the events. Let them fall through to the all
                      // apps view.
    }//from www  .  ja va2s.c o  m

    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
        return true;
    }

    acquireVelocityTrackerAndAddMovement(ev);

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {

        /*
         * Locally do absolute value. mLastMotionX is set to the y value of the
         * down event.
         */
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);

        if (pointerIndex < 0) {
            // invalid pointer
            return true;
        }

        final float x = ev.getX(pointerIndex);
        final float y = ev.getY(pointerIndex);
        final int xDiff = (int) Math.abs(x - mLastMotionX);
        final int yDiff = (int) Math.abs(y - mLastMotionY);

        final int touchSlop = mTouchSlop;
        boolean xMoved = xDiff > touchSlop;
        boolean yMoved = yDiff > touchSlop;
        mLastMotionX2 = x;

        if (xMoved || yMoved) {

            if (xMoved) {
                // Scroll if the user moved far enough along the X axis
                mTouchState = TOUCH_STATE_SCROLLING;
                mLastMotionX = x;
                mTouchX = getScrollX();
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
            }

        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mLastMotionX = x;
        mLastMotionX2 = x;
        mLastMotionY = y;
        mActivePointerId = ev.getPointerId(0);
        mAllowLongPress = true;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        // Release the drag
        clearChildrenCache();
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        mAllowLongPress = false;
        releaseVelocityTracker();
        break;

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

    /*
     * The only time we want to intercept motion events is if we are in the drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}

From source file:com.melolchik.bullshitbingo.ui.views.swipe.BottomSwipeRefreshLayout.java

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

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    //log("onInterceptTouchEvent mNestedScrollInProgress = " + mNestedScrollInProgress);
    // log("canChildScrollDown() = " + canChildScrollDown());
    if (!isEnabled() || mReturningToStart || canChildScrollDown() || mRefreshing || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        //log("onInterceptTouchEvent false action = " + action);
        return false;
    }

    //log("onInterceptTouchEvent cant scroll down action = " + action);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setTargetOffsetTopAndBottom(0/*mOriginalOffsetTop + mCircleView.getTop()*/, true);
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragged = false;

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

    case MotionEvent.ACTION_MOVE:
        //log("ACTION_MOVE");
        if (mActivePointerId == INVALID_POINTER) {
            log("ACTION_MOVE Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            log("ACTION_MOVE 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:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
}

From source file:com.dirkgassen.wator.ui.view.RangeSlider.java

/**
 * Handle touch events: drag the thumb to new values. This method also detects clicks.
 *
 * @param event the touch event//from  w w  w  . java  2 s .  c  om
 * @return {@code true} if the event was handled; {@code false} otherwise
 */
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    if (Log.isLoggable("Wa-Tor", Log.DEBUG)) {
        Log.d("Wa-Tor", "Got touch event: " + event);
    }
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        touchEventStartTime = System.currentTimeMillis();
        valueBeforeDragging = value;
        activePointerId = event.getPointerId(0);
        float valuePos = positionFromValue(paddingLeft, paddingRight);
        float x = event.getX();
        if (x < valuePos - thumbSize / 2 || x > valuePos + thumbSize / 2) {
            int newValue = valueFromPosition(x, paddingLeft, paddingRight);
            if (Log.isLoggable("Wa-Tor", Log.DEBUG)) {
                Log.d("Wa-Tor", "Starting to drag thumb OUTSIDE of thumb");
            }
            if (newValue != value) {
                touchEventStartTime = 0L; // Not a click
                updateValue(newValue, true /* from user */);
            }
            touchOffset = 0f;
        } else {
            touchOffset = x - valuePos;
            if (Log.isLoggable("Wa-Tor", Log.DEBUG)) {
                Log.d("Wa-Tor", "Starting to drag thumb INSIDE of thumb; offset = " + touchOffset);
            }
        }
        isDragging = true;
        return true;
    case MotionEvent.ACTION_MOVE:
        if (activePointerId != -1) {
            final int pointerIndex = event.findPointerIndex(activePointerId);
            float currentPos = event.getX(pointerIndex) - touchOffset;
            int newValue = valueFromPosition(currentPos, paddingLeft, paddingRight);
            if (newValue != value) {
                if (Log.isLoggable("Wa-Tor", Log.DEBUG)) {
                    Log.d("Wa-Tor", "Got new value " + newValue + " (old = " + value + ")");
                }
                touchEventStartTime = 0L; // Not a click
                updateValue(newValue, true /* from user */);
            }
        }
        return true;
    case MotionEvent.ACTION_UP:
        if (touchEventStartTime > 0L && System.currentTimeMillis() - touchEventStartTime < MAX_CLICK_DURATION) {
            performClick();
        }
        isDragging = false;
        break;
    case MotionEvent.ACTION_CANCEL:
        isDragging = false;
        updateValue(valueBeforeDragging, true /* from user */);
        break;
    case MotionEvent.ACTION_POINTER_UP:
        isDragging = false;
        break;
    }

    return super.onTouchEvent(event);
}

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

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*  w  w w. j  av a 2s . com*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp() || isRefreshing() || nestedScrollInProgress) {
        // 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);
        isBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        pointerIndex = ev.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 = ev.getY(pointerIndex);
        startDragging(y);

        if (isBeingDragged) {
            final float overScrollTop = (y - mInitialMotionY) * DRAG_RATE;
            if (overScrollTop > 0 && !isLoadingMore()) {
                //LogUtil.e("onTouchEvent ... ");
                moveHead(overScrollTop);
            } else {
                return false;
            }
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        pointerIndex = MotionEventCompat.getActionIndex(ev);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_POINTER_DOWN event but have an invalid action index.");
            return false;
        }
        mActivePointerId = ev.getPointerId(pointerIndex);
        break;
    }

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

    case MotionEvent.ACTION_UP: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            return false;
        }
        if (isBeingDragged) {
            final float y = ev.getY(pointerIndex);
            final float overScrollTop = (y - mInitialMotionY) * DRAG_RATE;
            isBeingDragged = false;
            //LogUtil.e("currentTargetOffsetTop = " + currentTargetOffsetTop);
            finishHead(overScrollTop);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    case MotionEvent.ACTION_CANCEL:
        return false;
    }

    return true;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();/*w w  w  .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() || mReturningToStart || canChildScrollUp() || isRefreshing() || nestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (!isLoadingMore()) {
            setTargetOffsetTopAndBottom(originalOffsetTop - circleView.getTop(), true);
        }
        mActivePointerId = ev.getPointerId(0);
        isBeingDragged = false;

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

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
            return false;
        }

        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:
    case MotionEvent.ACTION_CANCEL:
        isBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return isBeingDragged;
}

From source file:org.alex.swiperefreshlayout.SwipeRefreshLayout.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from w ww  . j a v a2s .com*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp() || isRefreshing() || nestedScrollInProgress) {
        // 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);
        isBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        pointerIndex = ev.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 = ev.getY(pointerIndex);
        startDragging(y);

        if (isBeingDragged) {
            final float overScrollTop = (y - mInitialMotionY) * DRAG_RATE;
            if (overScrollTop > 0 && !isLoadingMore()) {
                //LogUtil.e("onTouchEvent ... ");
                moveHead(overScrollTop);
            } else {
                return false;
            }
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        pointerIndex = MotionEventCompat.getActionIndex(ev);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_POINTER_DOWN event but have an invalid action index.");
            return false;
        }
        mActivePointerId = ev.getPointerId(pointerIndex);
        break;
    }

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

    case MotionEvent.ACTION_UP: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            return false;
        }
        if (isBeingDragged) {
            final float y = ev.getY(pointerIndex);
            final float overScrollTop = (y - mInitialMotionY) * DRAG_RATE;
            isBeingDragged = false;
            LogUtil.e("currentTargetOffsetTop = " + currentTargetOffsetTop);
            finishHead(overScrollTop);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    case MotionEvent.ACTION_CANCEL:
        return false;
    }

    return true;
}

From source file:net.simonvt.staggeredgridview.StaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    velocityTracker.addMovement(ev);//  w  w  w. j  a va2s.  c  om
    final int action = ev.getAction() & MotionEvent.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        velocityTracker.clear();
        scroller.abortAnimation();
        lastTouchY = ev.getY();
        lastTouchX = ev.getX();
        activePointerId = ev.getPointerId(0);
        touchRemainderY = 0;
        if (touchMode == TOUCH_MODE_FLINGING) {
            // Catch!
            touchMode = TOUCH_MODE_DRAGGING;
            return true;
        }
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = ev.findPointerIndex(activePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + activePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = ev.getY(index);
        final float dy = y - lastTouchY + touchRemainderY;
        final int deltaY = (int) dy;
        touchRemainderY = dy - deltaY;

        if (Math.abs(dy) > touchSlop) {
            touchMode = TOUCH_MODE_DRAGGING;
            return true;
        }
    }
    }

    return false;
}

From source file:cn.ismartv.tvrecyclerview.widget.helper.ItemTouchHelper.java

private ViewHolder findSwipedView(MotionEvent motionEvent) {
    final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return null;
    }//from   w  ww . j  av  a 2 s  .co m
    final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId);
    final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX;
    final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY;
    final float absDx = Math.abs(dx);
    final float absDy = Math.abs(dy);

    if (absDx < mSlop && absDy < mSlop) {
        return null;
    }
    if (absDx > absDy && lm.canScrollHorizontally()) {
        return null;
    } else if (absDy > absDx && lm.canScrollVertically()) {
        return null;
    }
    View child = findChildView(motionEvent);
    if (child == null) {
        return null;
    }
    return mRecyclerView.getChildViewHolder(child);
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

private int preProcessScrollMotionEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (DEBUG_TOUCH)
            Log.d(TAG, "DOWN: " + ev.getX(0) + "," + ev.getY(0));
        if (!mIsScrolling) {
            mVelocityTracker.clear();/*from   w ww . j a va 2s.  c  o  m*/
            mDownEvent.mCoords.x = ev.getX(0);
            mDownEvent.mCoords.y = ev.getY(0);
            mDownEvent.mPointerId = ev.getPointerId(0);
            if (DEBUG_TOUCH)
                Log.d(TAG, "SAVED: " + ev.getX(0) + "," + ev.getY(0));
        }
        break;
    case MotionEvent.ACTION_MOVE: {
        final int ptrIdx = ev.findPointerIndex(mDownEvent.mPointerId);
        if (ptrIdx < 0 || ptrIdx >= ev.getPointerCount())
            return -1;

        if (DEBUG_TOUCH)
            Log.d(TAG, "MOVE: " + ev.getX(ptrIdx) + "," + ev.getY(ptrIdx));
        if (mIsScrolling) {
            return mDrawerState.mActiveDrawer;
        }

        // If the user has dragged her finger horizontally more than
        // the touch slop, start the scroll

        final float xDiff = calculateDistanceX(ev, mDownEvent);
        final float yDiff = calculateDistanceY(ev, mDownEvent);
        final float dxAbs = Math.abs(xDiff);
        final float dyAbs = Math.abs(yDiff);

        if (DEBUG_TOUCH) {
            Log.d(TAG, "xDiff: " + xDiff + " -- " + ev.getX(ptrIdx) + " -- " + mDownEvent.mCoords.x);
            Log.d(TAG, "xDiff/yDiff/dxAbs/dyAbs: " + xDiff + "/" + yDiff + "/" + dxAbs + "/" + dyAbs);
            Log.d(TAG, "canScrollX/canScrollY: " + canScroll(MotionEvent.AXIS_X, xDiff) + "/"
                    + canScroll(MotionEvent.AXIS_Y, yDiff));
            Log.d(TAG, "activeDrawer/scrollState: " + mDrawerState.mActiveDrawer + "/"
                    + mDrawerState.mScrollState);
        }

        if (dxAbs > dyAbs && dxAbs > mTouchSlop && canScroll(MotionEvent.AXIS_X, xDiff)) {
            if (DEBUG_TOUCH)
                Log.d(TAG, "X axis branch");
            int drawer;
            if (mDrawerState.mActiveDrawer == NO_DRAWER)
                drawer = xDiff > 0 ? LEFT_DRAWER : RIGHT_DRAWER;
            else
                drawer = mDrawerState.mActiveDrawer;
            if (!isInitialPositionValidForDrawer(mDownEvent, drawer))
                drawer = -1;
            return drawer;
        }

        if (dyAbs > dxAbs && dyAbs > mTouchSlop && canScroll(MotionEvent.AXIS_Y, yDiff)) {
            if (DEBUG_TOUCH)
                Log.d(TAG, "Y axis branch");
            int drawer;
            if (mDrawerState.mActiveDrawer == NO_DRAWER)
                drawer = yDiff > 0 ? TOP_DRAWER : BOTTOM_DRAWER;
            else
                drawer = mDrawerState.mActiveDrawer;

            if (!isInitialPositionValidForDrawer(mDownEvent, drawer))
                drawer = -1;
            return drawer;
        }

        break;
    }
    }
    return -1;
}