Example usage for android.support.v4.view MotionEventCompat getActionMasked

List of usage examples for android.support.v4.view MotionEventCompat getActionMasked

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat getActionMasked.

Prototype

public static int getActionMasked(MotionEvent event) 

Source Link

Document

Call MotionEvent#getAction , returning only the #ACTION_MASK portion.

Usage

From source file:com.tienpx.videodrag.DraggableView.java

/**
 * Override method to dispatch touch event to the dragged view.
 *
 * @param ev captured./* w w w  .  ja  v  a2s.  c o m*/
 * @return true if the touch event is realized over the drag or second view.
 */
@Override
public boolean onTouchEvent(MotionEvent ev) {
    int actionMasked = MotionEventCompat.getActionMasked(ev);
    if ((actionMasked & MotionEventCompat.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
        activePointerId = MotionEventCompat.getPointerId(ev, actionMasked);
    }
    if (activePointerId == INVALID_POINTER) {
        return false;
    }
    viewDragHelper.processTouchEvent(ev);
    if (isClosed()) {
        return false;
    }
    boolean isDragViewHit = isViewHit(dragView, (int) ev.getX(), (int) ev.getY());
    boolean isSecondViewHit = isViewHit(secondView, (int) ev.getX(), (int) ev.getY());
    analyzeTouchToMaximizeIfNeeded(ev, isDragViewHit);
    if (isMaximized()) {
        dragView.dispatchTouchEvent(ev);
    } else {
        dragView.dispatchTouchEvent(cloneMotionEventWithAction(ev, MotionEvent.ACTION_CANCEL));
    }
    return isDragViewHit || isSecondViewHit;
}

From source file:com.taobao.luaview.view.widget.SuperSwipeRefreshLayout.java

/**
 * ???View<br>//from  w  w w .  j  av  a 2  s  .c  o m
 * OnTouchEvent?<br>
 * ??View?<br>
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || mReturningToStart || mRefreshing || !isChildScrollToTop()) {
        // ?View???View?-
        // ?View?-
        return false;
    }

    // 
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mHeadViewContainer.getTop(), true);// ??HeaderView??
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialMotionY = getMotionEventY(ev, mActivePointerId);
        if (initialMotionY == -1) {
            return false;
        }
        mInitialMotionY = initialMotionY;// ?

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

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        float yDiff = y - mInitialMotionY;// ?
        if (yDiff > mTouchSlop && !mIsBeingDragged) {// ??
            mIsBeingDragged = true;// 
        }
        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;// ?View
}

From source file:com.cyj.ui.component.listview.CusSwipeRefreshLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();// ww  w  .j  a  v  a  2  s  . co  m

    final int action = MotionEventCompat.getActionMasked(ev);

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

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mIsPulling = false;
        mCanRefreshing = false;
        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;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;
        if (yDiff > mTouchSlop) {
            mLastMotionY = y;
            mIsBeingDragged = true;
        }
        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:chinanurse.cn.nurse.list.WaveSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    if (!isEnabled() || canChildScrollUp()) {
        return false;
    }// w ww. j  a v  a  2  s  .  c  o m
    mIsBeingDropped = mWaveView.isDisappearCircleAnimatorRunning();

    final int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Here is not called from anywhere
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(event, mActivePointerId);
        return pointerIndex >= 0 && onMoveTouchEvent(event, pointerIndex);

    case MotionEvent.ACTION_UP:
        if (mIsBeingDropped) {
            mIsBeingDropped = false;
            return false;
        }

        final float diffY = event.getY() - mFirstTouchDownPointY;
        final float waveHeightThreshold = diffY
                * (5f - 2 * diffY / Math.min(getMeasuredWidth(), getMeasuredHeight())) / 1000f;
        mWaveView.startWaveAnimation(waveHeightThreshold);

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

        if (!isRefreshing()) {
            mCircleView.setProgressStartEndTrim(0f, 0f);
            mCircleView.showArrow(false);
            mCircleView.setVisibility(GONE);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    return true;
}

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 .ja  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:com.jude.easyrecyclerview.waveswiperefreshlayout.WaveSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    if (!isEnabled() || canChildScrollUp()) {
        return false;
    }// w  w w.  ja  va  2s  . c  o  m
    mIsBeingDropped = mWaveView.isDisappearCircleAnimatorRunning();

    final int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Here is not called from anywhere
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(event, mActivePointerId);
        return pointerIndex >= 0 && onMoveTouchEvent(event, pointerIndex);

    case MotionEvent.ACTION_UP:
        if (mIsBeingDropped) {
            mIsBeingDropped = false;
            return false;
        }

        final float diffY = event.getY() - mFirstTouchDownPointY;
        final float waveHeightThreshold =
                //            diffY * (5f - 2 * diffY / Math.min(getMeasuredWidth(), getMeasuredHeight())) / 1000f;
                diffY * (5f - 2 * diffY / Math.min(getMeasuredWidth(), getMeasuredHeight())) / 1000f;
        mWaveView.startWaveAnimation(waveHeightThreshold);

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

        if (!isRefreshing()) {
            mCircleView.setProgressStartEndTrim(0f, 0f);
            mCircleView.showArrow(false);
            mCircleView.setVisibility(GONE);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    return true;
}

From source file:com.coco.slidinguppanel.SlidingUpPanel.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getState() == STATE_OPENED) {
        // disable touch handle when in opened state.
        return false;
    }//from   w  ww.j ava 2s.c o  m

    final int action = MotionEventCompat.getActionMasked(ev);

    if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our descendants.
        return false;
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev, false);
        DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER || mIsUnableToDrag) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float xDiff = Math.abs(x - mInitialMotionX);
        final float yDiff = Math.abs(y - mInitialMotionY);
        DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        onTouchMove(x, y, xDiff, yDiff, false);
        break;
    }
    case MotionEvent.ACTION_UP: {
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                    mActivePointerId);
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final int totalDelta = (int) (y - mInitialMotionY);
            boolean toOpen = determineToOpen(initialVelocity, totalDelta);
            startFling(toOpen, initialVelocity);
            endDrag();
        }
        DEBUG_LOG("Touch up!!!");
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        if (mIsBeingDragged) {
            startFling(isOpen(), 0);
            endDrag();
        }
        DEBUG_LOG("Touch cancel!!!");
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onTouchPointerUp(ev);
        break;
    }

    return true;
}

From source file:com.example.carlitos.swipeitemrecycler.view.animation.swipe_item.swipeable.RecyclerViewSwipeManager.java

private boolean handleActionUpOrCancel(MotionEvent e, boolean invokeFinish) {
    int action = MotionEvent.ACTION_CANCEL;

    if (e != null) {
        action = MotionEventCompat.getActionMasked(e);
        mLastTouchX = (int) (e.getX() + 0.5f);
        mLastTouchY = (int) (e.getY() + 0.5f);
    }//from   w  w w.  j av  a  2  s.  c  o  m

    if (isSwiping()) {
        if (invokeFinish) {
            handleActionUpOrCancelWhileSwiping(action);
        }
        return true;
    } else {
        handleActionUpOrCancelWhileNotSwiping();
        return false;
    }
}

From source file:cn.colink.commumication.swipelistview.SwipeListView.java

/**
 * @see android.widget.ListView#onInterceptTouchEvent(android.view.MotionEvent)
 *///  w ww.  j  av  a2s.c  om
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();

    if (touchState == TOUCH_STATE_SCROLLING_X) {
        return touchListener.onTouch(this, ev);
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE:
        checkInMoving(x, y);
        return touchState == TOUCH_STATE_SCROLLING_Y;
    case MotionEvent.ACTION_DOWN:
        touchListener.onTouch(this, ev);
        touchState = TOUCH_STATE_REST;
        lastMotionX = x;
        lastMotionY = y;
        return false;
    case MotionEvent.ACTION_CANCEL:
        touchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_UP:
        touchListener.onTouch(this, ev);
        return touchState == TOUCH_STATE_SCROLLING_Y;
    default:
        break;
    }

    return super.onInterceptTouchEvent(ev);
}

From source file:com.hayukleung.app.view.refresh.SwipeRefreshLayout.java

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

    final int action = MotionEventCompat.getActionMasked(ev);

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

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        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;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }
        if (mRefreshing) {
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;
        if (yDiff > mTouchSlop) {
            mLastMotionY = y;
            mIsBeingDragged = true;
        }
        break;

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

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

    return mIsBeingDragged;
}