Example usage for android.view MotionEvent obtain

List of usage examples for android.view MotionEvent obtain

Introduction

In this page you can find the example usage for android.view MotionEvent obtain.

Prototype

static public MotionEvent obtain(MotionEvent other) 

Source Link

Document

Create a new MotionEvent, copying from an existing one.

Usage

From source file:cn.bvin.app.swiperefresh.OldSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;/*from w w  w .j  a v a  2 s  .c om*/
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {//?
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {//??
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    //content????
                    updateContentOffsetTop((int) (offsetTop * mResistance));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                    } else {
                        updatePositionTimeout();
                    }
                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}

From source file:com.jlstudio.main.widget.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;//from w w w .  ja va2s . co m
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        //               AppLog.e(mTarget.getBottom()+"  bottom");
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (!canChildScrollDown() && yDiff < 0) {
                yDiff = -eventY + mDownEvent.getY();
                if (yDiff > mTouchSlop) {
                    // User velocity passed min velocity; trigger a refresh
                    if (yDiff > mDistanceToTriggerSync) {
                        // User movement passed distance; trigger a refresh
                        startRefreshUp();
                        handled = true;
                        break;
                    } else {
                        // Just track the user's movement
                        setTriggerPercentage(
                                mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                        float offsetTop = yDiff;
                        if (mPrevY > eventY) {
                            offsetTop = yDiff - mTouchSlop;
                        }
                        updateContentOffsetTop((int) (-offsetTop));
                        if (mPrevY > eventY && (mTarget.getBottom() < mTouchSlop)) {
                            // If the user puts the view back at the top, we
                            // don't need to. This shouldn't be considered
                            // cancelling the gesture as the user can restart from the top.
                            removeCallbacks(mCancel);
                        } else {
                            updatePositionTimeout();
                        }
                        mPrevY = event.getY();
                        handled = true;
                    }
                }
            } else {
                if (yDiff > mTouchSlop) {
                    // User velocity passed min velocity; trigger a refresh
                    if (yDiff > mDistanceToTriggerSync) {
                        // User movement passed distance; trigger a refresh
                        startRefresh();
                        handled = true;
                        break;
                    } else {
                        // Just track the user's movement
                        setTriggerPercentage(
                                mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                        float offsetTop = yDiff;
                        if (mPrevY > eventY) {
                            offsetTop = yDiff - mTouchSlop;
                        }
                        updateContentOffsetTop((int) (offsetTop));
                        if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                            // If the user puts the view back at the top, we
                            // don't need to. This shouldn't be considered
                            // cancelling the gesture as the user can restart from the top.
                            removeCallbacks(mCancel);
                        } else {
                            updatePositionTimeout();
                        }
                        mPrevY = event.getY();
                        handled = true;
                    }
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}

From source file:com.dm.xz.views.PinnedSectionListView.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    final float x = ev.getX();
    final float y = ev.getY();
    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_DOWN && mTouchTarget == null && mPinnedSection != null
            && isPinnedViewTouched(mPinnedSection.view, x, y)) { // create touch target

        // user touched pinned view
        mTouchTarget = mPinnedSection.view;
        mTouchPoint.x = x;//w w w  . jav a 2s. c o  m
        mTouchPoint.y = y;

        // copy down event for eventually be used later
        mDownEvent = MotionEvent.obtain(ev);
    }

    if (mTouchTarget != null) {
        if (isPinnedViewTouched(mTouchTarget, x, y)) { // forward event to pinned view
            mTouchTarget.dispatchTouchEvent(ev);
        }

        if (action == MotionEvent.ACTION_UP) { // perform onClick on pinned view
            super.dispatchTouchEvent(ev);
            performPinnedItemClick();
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_CANCEL) { // cancel
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_MOVE) {
            if (Math.abs(y - mTouchPoint.y) > mTouchSlop) {

                // cancel sequence on touch target
                MotionEvent event = MotionEvent.obtain(ev);
                event.setAction(MotionEvent.ACTION_CANCEL);
                mTouchTarget.dispatchTouchEvent(event);
                event.recycle();

                // provide correct sequence to super class for further handling
                super.dispatchTouchEvent(mDownEvent);
                super.dispatchTouchEvent(ev);
                clearTouchTarget();

            }
        }

        return true;
    }

    // call super if this was not our pinned view
    return super.dispatchTouchEvent(ev);
}

From source file:de.telekom.pde.codelibrary.ui.layout.PDESwipeRefreshLayout.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;//from w w w .j  a va2s .  c o  m
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    updateContentOffsetTop((int) (offsetTop));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                    } else {
                        updatePositionTimeout();
                    }
                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}

From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.swipedismiss.SwipeTouchListener.java

private boolean handleMoveEvent(@Nullable final View view, @NonNull final MotionEvent motionEvent) {
    if (mVelocityTracker == null || mCurrentView == null) {
        return false;
    }//from  w  w  w .  j  a  v a2  s. c  o  m

    //  ?? ?? ?  ? ? 
    mVelocityTracker.addMovement(motionEvent);

    //  ? ?
    float deltaX = motionEvent.getX() - mDownX;
    float deltaY = motionEvent.getY() - mDownY;

    boolean dismissToRight = deltaX < 0;
    mflingToRight = dismissToRight;

    // ? ?  ?? ?,  ??  ? ? ??? ?
    if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {

        // ? ? ??
        if (!mSwiping) {
            mActiveSwipeCount++;
        }

        // ? ?? 
        mSwiping = true;
        mListViewWrapper.getListView().requestDisallowInterceptTouchEvent(true);

        /* Cancel ListView's touch (un-highlighting the item) */
        if (view != null) {
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
            view.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }
    }

    // ????
    if (mSwiping) {

        // ? ?
        if (mCanDismissCurrent) {

            // ? ?? 
            onStartSwipe(mCurrentView, mCurrentPosition, dismissToRight);

            //   ??
            ViewHelper.setTranslationX(mSwipingView, deltaX);

            // ??? ? .
            //ViewHelper.setAlpha(mSwipingView, Math.max(mMinimumAlpha, Math.min(1, 1 - 2 * Math.abs(deltaX) / mViewWidth)));

            if (dismissToRight) {

                mLeftSwipingView.setVisibility(View.GONE);
                mRightSwipingView.setVisibility(View.VISIBLE);
            } else {
                mLeftSwipingView.setVisibility(View.VISIBLE);
                mRightSwipingView.setVisibility(View.GONE);
            }

        } else {

            //  ? ? ?
            ViewHelper.setTranslationX(mSwipingView, deltaX * 0.1f);
        }
        return true;
    }
    return false;
}

From source file:com.daiv.android.twitter.manipulations.widgets.swipe_refresh_layout.FullScreenSwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;//from w  w  w.  j av  a2  s  . c om
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    offsetTop = offsetTop / 6;
                    updateContentOffsetTop((int) (offsetTop));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                        resetTriggerPercentage();
                        updateContentOffsetTop(0);
                    } else {
                        updatePositionTimeout();
                    }
                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}

From source file:com.taobao.weex.ui.view.WXScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!scrollable) {
        return true; // when scrollable is set to false, then eat the touch event
    }/*from  w  ww.j a va2s .  com*/
    if (mRedirectTouchToStickyView) {

        if (mScrollRect == null) {
            mScrollRect = new Rect();
            getGlobalVisibleRect(mScrollRect);
        }
        mCurrentStickyView.getLocationOnScreen(stickyViewP);
        ev.offsetLocation(0, -(stickyViewP[1] - mScrollRect.top));
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mHasNotDoneActionDown = false;
    }

    if (mHasNotDoneActionDown) {
        MotionEvent down = MotionEvent.obtain(ev);
        down.setAction(MotionEvent.ACTION_DOWN);
        mHasNotDoneActionDown = false;
        down.recycle();
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        ox = ev.getX();
        oy = ev.getY();
        // Dispatch touch event to parent view
        startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL);
    }

    if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        mHasNotDoneActionDown = true;
        // stop nested scrolling dispatch
        stopNestedScroll();
    }

    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        float clampedX = ev.getX();
        float clampedY = ev.getY();
        int dx = (int) (ox - clampedX);
        int dy = (int) (oy - clampedY);

        if (dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)) {
            // sub dx/dy was consumed by parent view!!!
            ev.setLocation(clampedX + consumed[0], clampedY + consumed[1]);
        }
        ox = ev.getX();
        oy = ev.getY();
    }

    boolean result = super.onTouchEvent(ev);
    if (wxGesture != null) {
        result |= wxGesture.onTouch(this, ev);
    }
    return result;
}

From source file:com.bitants.wally.views.swipeclearlayout.SwipeClearLayout.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        currPercentage = 0;/*from w  w  w  .j av  a  2  s . c o m*/
        downEvent = MotionEvent.obtain(event);
        prevY = downEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (downEvent != null && !returningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - downEvent.getY();
            if (yDiff > touchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > distanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            accelerateInterpolator.getInterpolation(yDiff / distanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (prevY > eventY) {
                        offsetTop = yDiff - touchSlop;
                    }
                    updateContentOffsetTop((int) (offsetTop));
                    //                            if (prevY > eventY && (target.getTop() < touchSlop)) {
                    if (prevY > eventY && (circle.getTop() < touchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(cancel);
                    } else {
                        updatePositionTimeout();
                    }
                    prevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (downEvent != null) {
            downEvent.recycle();
            downEvent = null;
        }
        break;
    }
    return handled;
}

From source file:com.gome.haoyuangong.views.SwipeRefreshLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    ensureTarget();/* ww  w  .  j a  v  a2 s .c om*/
    boolean handled = false;
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mInterceptY = (int) ev.getY();
    }
    if (isEnabled() && !canChildScroll(this, (int) ev.getY() - mInterceptY, 1)) {
        if (mReturningToStart && ev.getAction() == MotionEvent.ACTION_DOWN) {
            mReturningToStart = false;
            removeAnimateMove();
        }
        handled = onTouchEvent(ev);
    } else {
        mDownEvent = MotionEvent.obtain(ev);
        mDownTargetTop = mCurrentTargetOffsetTop;
    }
    mInterceptY = (int) ev.getY();
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        if (mOldHandled == 1 && !handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_DOWN);
            getChildAt(1).dispatchTouchEvent(event);
            disableSubControls(getChildAt(1));
            event.recycle();
        } else if (mOldHandled == 2 && handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_CANCEL);
            getChildAt(1).dispatchTouchEvent(event);
            event.recycle();
        } else if (mOldHandled == 0 && handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_CANCEL);
            getChildAt(1).dispatchTouchEvent(event);
            event.recycle();
        }
        if (handled) {
            mOldHandled = 1;
        } else {
            mOldHandled = 2;
        }
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        mOldHandled = 0;
    }
    if (!handled) {
        super.dispatchTouchEvent(ev);
    }
    return true;
}

From source file:com.miku.framelite.widget.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;/*from  www. ja v a 2  s  .  co  m*/
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            final boolean isSwipeDown = mSwipeMode == MODE_SWIPE_DOWN;
            float yDiff = isSwipeDown ? (eventY - mDownEvent.getY()) : (mDownEvent.getY() - eventY);
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offset = yDiff;

                    if (isSwipeDown) {
                        if (mPrevY > eventY) {
                            offset = yDiff - mTouchSlop;
                        }
                        updateContentOffsetTop((int) (offset));
                        if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                            // If the user puts the view back at the top, we
                            // don't need to. This shouldn't be considered
                            // cancelling the gesture as the user can restart
                            // from the top.
                            removeCallbacks(mCancel);
                        } else {
                            updatePositionTimeout();
                        }
                    } else {
                        if (eventY > mPrevY) {
                            offset = yDiff - mTouchSlop;
                        }
                        updateContentOffsetTop((int) (offset));
                        //                     if (eventY > mPrevY && (-mTarget.getTop() > mTouchSlop)) {
                        //                        // If the user puts the view back at the top, we
                        //                        // don't need to. This shouldn't be considered
                        //                        // cancelling the gesture as the user can restart
                        //                        // from the top.
                        //                        removeCallbacks(mCancel);
                        //                     } else {
                        updatePositionTimeout();
                        //                     }
                    }

                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}