Java Code Examples for androidx.core.view.MotionEventCompat#getY()

The following examples show how to use androidx.core.view.MotionEventCompat#getY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuperRecyclerView.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (mRefreshEnabled) {
        final int action = MotionEventCompat.getActionMasked(e);
        final int actionIndex = MotionEventCompat.getActionIndex(e);
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, 0);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEvent.ACTION_POINTER_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, actionIndex);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEventCompat.ACTION_POINTER_UP: {
                onPointerUp(e);
            }
            break;
        }
    }
    return super.onInterceptTouchEvent(e);
}
 
Example 2
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
private ViewHolder findSwipedView(MotionEvent motionEvent) {
    final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return null;
    }
    final int pointerIndex = MotionEventCompat.findPointerIndex(motionEvent, mActivePointerId);
    final float dx = MotionEventCompat.getX(motionEvent, pointerIndex) - mInitialTouchX;
    final float dy = MotionEventCompat.getY(motionEvent, 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);
}
 
Example 3
Source File: ViewDragHelper.java    From react-native-photo-editor with Apache License 2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
        if (mLastMotionX != null && mLastMotionY != null
                && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
            mLastMotionX[pointerId] = x;
            mLastMotionY[pointerId] = y;
        }
    }
}
 
Example 4
Source File: ViewDragHelper.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
        if (mLastMotionX != null && mLastMotionY != null
                && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
            mLastMotionX[pointerId] = x;
            mLastMotionY[pointerId] = y;
        }
    }
}
 
Example 5
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
public void onLongPress(MotionEvent e) {
    View child = findChildView(e);
    if (child != null) {
        ViewHolder vh = mRecyclerView.getChildViewHolder(child);
        if (vh != null) {
            if (!mCallback.hasDragFlag(mRecyclerView, vh)) {
                return;
            }
            int pointerId = MotionEventCompat.getPointerId(e, 0);
            // Long press is deferred.
            // Check w/ active pointer id to avoid selecting after motion
            // event is canceled.
            if (pointerId == mActivePointerId) {
                final int index = MotionEventCompat
                        .findPointerIndex(e, mActivePointerId);
                final float x = MotionEventCompat.getX(e, index);
                final float y = MotionEventCompat.getY(e, index);
                mInitialTouchX = x;
                mInitialTouchY = y;
                mDx = mDy = 0f;
                if (DEBUG) {
                    Log.d(TAG,
                            "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY);
                }
                if (mCallback.isLongPressDragEnabled()) {
                    select(vh, ACTION_STATE_DRAG);
                }
            }
            handleTouch(e);
        }
    }
}
 
Example 6
Source File: VerticalViewPager.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, 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;
        mLastMotionY = MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}
 
Example 7
Source File: DragLayout.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
private float getMotionEventY(MotionEvent ev, int activePointerId) {
    final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
    if (index < 0) {
        return -1;
    }
    return MotionEventCompat.getY(ev, index);
}
 
Example 8
Source File: ViewDragHelper.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
        if (mLastMotionX != null && mLastMotionY != null
                && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
            mLastMotionX[pointerId] = x;
            mLastMotionY[pointerId] = y;
        }
    }
}
 
Example 9
Source File: PullRefreshLayout.java    From QuickDevFramework with Apache License 2.0 5 votes vote down vote up
private float getMotionY(MotionEvent ev, int activePointerId) {
    final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
    if (index < 0) {
        return -1;
    }
    return MotionEventCompat.getY(ev, index);
}
 
Example 10
Source File: SlidingLayout.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
private float getMotionEventY(MotionEvent ev, int activePointerId) {
    final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
    if (index < 0) {
        return -1;
    }
    return MotionEventCompat.getY(ev, index);
}
 
Example 11
Source File: ViewDragHelper.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
private void saveLastMotion(@NonNull MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
        if (mLastMotionX != null && mLastMotionY != null
                && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
            mLastMotionX[pointerId] = x;
            mLastMotionY[pointerId] = y;
        }
    }
}
 
Example 12
Source File: ViewDragHelper.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        mLastMotionX[pointerId] = x;
        mLastMotionY[pointerId] = y;
    }
}
 
Example 13
Source File: SuperRecyclerView.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
private int getMotionEventY(MotionEvent e, int pointerIndex) {
    return (int) (MotionEventCompat.getY(e, pointerIndex) + 0.5f);
}
 
Example 14
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
/**
 * Checks whether we should select a View for swiping.
 */
private boolean checkSelectForSwipe(int action, MotionEvent motionEvent, int pointerIndex) {
    if (mSelected != null || action != MotionEvent.ACTION_MOVE
            || mActionState == ACTION_STATE_DRAG || !mCallback.isItemViewSwipeEnabled()) {
        return false;
    }
    if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
        return false;
    }
    final ViewHolder vh = findSwipedView(motionEvent);
    if (vh == null) {
        return false;
    }
    final int movementFlags = mCallback.getAbsoluteMovementFlags(mRecyclerView, vh);

    final int swipeFlags = (movementFlags & ACTION_MODE_SWIPE_MASK)
            >> (DIRECTION_FLAG_COUNT * ACTION_STATE_SWIPE);

    if (swipeFlags == 0) {
        return false;
    }

    // mDx and mDy are only set in allowed directions. We use custom x/y here instead of
    // updateDxDy to avoid swiping if user moves more in the other direction
    final float x = MotionEventCompat.getX(motionEvent, pointerIndex);
    final float y = MotionEventCompat.getY(motionEvent, pointerIndex);

    // Calculate the distance moved
    final float dx = x - mInitialTouchX;
    final float dy = y - mInitialTouchY;
    // swipe target is chose w/o applying flags so it does not really check if swiping in that
    // direction is allowed. This why here, we use mDx mDy to check slope value again.
    final float absDx = Math.abs(dx);
    final float absDy = Math.abs(dy);

    if (absDx < mSlop && absDy < mSlop) {
        return false;
    }
    if (absDx > absDy) {
        if (dx < 0 && (swipeFlags & LEFT) == 0) {
            return false;
        }
        if (dx > 0 && (swipeFlags & RIGHT) == 0) {
            return false;
        }
    } else {
        if (dy < 0 && (swipeFlags & UP) == 0) {
            return false;
        }
        if (dy > 0 && (swipeFlags & DOWN) == 0) {
            return false;
        }
    }
    mDx = mDy = 0f;
    mActivePointerId = MotionEventCompat.getPointerId(motionEvent, 0);
    select(vh, ACTION_STATE_SWIPE);
    if (mPreOpened != null && mPreOpened != vh && vh != null) {
        closeOpenedPreItem();
    }
    return true;
}
 
Example 15
Source File: FlipView.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mIsFlippingEnabled) {
        return false;
    }

    if (mPageCount < 1) {
        return false;
    }

    final int action = ev.getAction() & MotionEvent.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL
            || action == MotionEvent.ACTION_UP) {
        mIsFlipping = false;
        mIsUnableToFlip = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsFlipping) {
            return true;
        } else if (mIsUnableToFlip) {
            return false;
        }
    }

    switch (action) {
        case MotionEvent.ACTION_MOVE:
            final int activePointerId = mActivePointerId;
            if (activePointerId == INVALID_POINTER) {
                break;
            }

            final int pointerIndex = MotionEventCompat.findPointerIndex(ev,
                    activePointerId);
            if (pointerIndex == -1) {
                mActivePointerId = INVALID_POINTER;
                break;
            }

            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float dx = x - mLastX;
            final float xDiff = Math.abs(dx);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float dy = y - mLastY;
            final float yDiff = Math.abs(dy);

            if ((mIsFlippingVertically && yDiff > mTouchSlop && yDiff > xDiff)
                    || (!mIsFlippingVertically && xDiff > mTouchSlop && xDiff > yDiff)) {
                mIsFlipping = true;
                mLastX = x;
                mLastY = y;
            } else if ((mIsFlippingVertically && xDiff > mTouchSlop)
                    || (!mIsFlippingVertically && yDiff > mTouchSlop)) {
                mIsUnableToFlip = true;
            }
            break;

        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getAction()
                    & MotionEvent.ACTION_POINTER_INDEX_MASK;
            mLastX = MotionEventCompat.getX(ev, mActivePointerId);
            mLastY = MotionEventCompat.getY(ev, mActivePointerId);

            mIsFlipping = !mScroller.isFinished() | mPeakAnim != null;
            mIsUnableToFlip = false;
            mLastTouchAllowed = true;

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

    if (!mIsFlipping) {
        trackVelocity(ev);
    }

    return mIsFlipping;
}