Example usage for android.view MotionEvent recycle

List of usage examples for android.view MotionEvent recycle

Introduction

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

Prototype

@Override
public final void recycle() 

Source Link

Document

Recycle the MotionEvent, to be re-used by a later caller.

Usage

From source file:org.creativecommons.thelist.swipedismiss.SwipeDismissRecyclerViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }//from  w ww.j  a v a 2s .  co m

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // TODO: ensure this is a finger, and set a flag

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    } //OnTouch

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            //TODO: DOES THIS WORK
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        mRefreshLayout.setEnabled(true);
        break;
    } //ACTION_CANCEL

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        //                float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        //                float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            //TODO: add animation lock
            //                    synchronized (mAnimationLock){
            //                        if(mAnimatedViews.contains(downView)){
            //                            break;
            //                        }
            //                        ++mDismissAnimationRefCount;
            //                        mAnimatedViews.add(downView);
            //                    }

            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime)
                    .setListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        mRefreshLayout.setEnabled(true);
        break;
    } //ACTION_UP

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mRefreshLayout.setEnabled(false);
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mRecyclerView.requestDisallowInterceptTouchEvent(true);

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

        if (mSwiping) {
            mDownView.setTranslationX(deltaX - mSwipingSlop);
            mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
            //                    setTranslationX(mDownView, deltaX);
            //                    setAlpha(mDownView, Math.max(0f, Math.min(1f,
            //                            1f - 2f * Math.abs(deltaX) / mViewWidth)));
            //                    return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.itude.mobile.mobbl.blueprint.app.view.listeners.SwipeDismissRecyclerViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    if (mViewWidth < 2) {
        mViewWidth = mIsVertical ? mRecyclerView.getHeight() : mRecyclerView.getWidth();
    }//from   w  ww.ja v a  2  s. c o  m

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // TODO: ensure this is a finger, and set a flag

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;

        mDownView = mRecyclerView.findChildViewUnder(x, y);

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();

            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            if (mIsVertical) {
                mDownView.animate().translationY(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            } else {
                mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            }
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (!mSwiping && mDownView != null && mItemTouchCallback != null) {

            mItemTouchCallback.onTouch(mRecyclerView.getChildPosition(mDownView));
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            mDownX = 0;
            mDownY = 0;
            mDownView = null;
            mDownPosition = ListView.INVALID_POSITION;
            mSwiping = false;
            return true;
        }

        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float velocityY = mVelocityTracker.getYVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;

        if (mIsVertical) {
            if (Math.abs(deltaY) > mViewWidth / 2 && mSwiping) {
                dismiss = true;
                dismissRight = deltaY > 0;
            } else if (mMinFlingVelocity <= absVelocityY && absVelocityY <= mMaxFlingVelocity
                    && absVelocityX < absVelocityY && mSwiping) {
                // dismiss only if flinging in the same direction as dragging
                dismiss = (velocityY < 0) == (deltaY < 0);
                dismissRight = mVelocityTracker.getYVelocity() > 0;
            }
            if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
                // dismiss
                final View downView = mDownView; // mDownView gets null'd before animation ends
                final int downPosition = mDownPosition;
                ++mDismissAnimationRefCount;
                mDownView.animate().translationY(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                        .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                performDismiss(downView, downPosition);
                            }
                        });
            } else {
                // cancel
                mDownView.animate().translationY(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            }
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            mDownX = 0;
            mDownY = 0;
            mDownView = null;
            mDownPosition = ListView.INVALID_POSITION;
            mSwiping = false;
        } else {

            if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
                dismiss = true;
                dismissRight = deltaX > 0;
            } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                    && absVelocityY < absVelocityX && mSwiping) {
                // dismiss only if flinging in the same direction as dragging
                dismiss = (velocityX < 0) == (deltaX < 0);
                dismissRight = mVelocityTracker.getXVelocity() > 0;
            }
            if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
                // dismiss
                final View downView = mDownView; // mDownView gets null'd before animation ends
                final int downPosition = mDownPosition;
                ++mDismissAnimationRefCount;
                mDownView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                        .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                performDismiss(downView, downPosition);
                            }
                        });
            } else {
                // cancel
                mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            }
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            mDownX = 0;
            mDownY = 0;
            mDownView = null;
            mDownPosition = ListView.INVALID_POSITION;
            mSwiping = false;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {

        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (mIsVertical) {
            if (Math.abs(deltaY) > mSlop && Math.abs(deltaX) < Math.abs(deltaY) / 2) {
                mSwiping = true;
                mSwipingSlop = (deltaY > 0 ? mSlop : -mSlop);
                mRecyclerView.requestDisallowInterceptTouchEvent(true);

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

            if (mSwiping) {
                mDownView.setTranslationY(deltaY - mSwipingSlop);
                mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaY) / mViewWidth)));
                return true;
            }

        } else {
            if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
                mSwiping = true;
                mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
                mRecyclerView.requestDisallowInterceptTouchEvent(true);

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

            if (mSwiping) {
                mDownView.setTranslationX(deltaX - mSwipingSlop);
                mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                return true;
            }
        }
        break;
    }
    }
    return false;
}

From source file:com.appunite.list.FastScroller.java

private void cancelFling() {
    // Cancel the list fling
    MotionEvent cancelFling = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
    mList.onTouchEvent(cancelFling);/*w ww. j  av  a2  s  . c o  m*/
    cancelFling.recycle();
}

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    ensureTarget();/*from w w  w  . j  ava 2 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.cw.litenote.note.Note.java

@Override
public void onBackPressed() {
    System.out.println("Note / _onBackPressed");
    // web view can go back
    String tagStr = "current" + viewPager.getCurrentItem() + "linkWebView";
    CustomWebView linkWebView = (CustomWebView) viewPager.findViewWithTag(tagStr);
    if (linkWebView.canGoBack()) {
        linkWebView.goBack();//from  w  w w .  j a va  2  s .  c o  m
    } else if (isPictureMode()) {
        // dispatch touch event to show buttons
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        float x = 0.0f;
        float y = 0.0f;
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        int metaState = 0;
        MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState);
        dispatchTouchEvent(event);
        event.recycle();

        // in order to make sure ImageViewBackButton is effective to be clicked
        mPagerHandler = new Handler();
        mPagerHandler.postDelayed(mOnBackPressedRun, 500);
    } else if (isTextMode()) {
        // back to view all mode
        setViewAllMode();
        setOutline(act);
    } else {
        System.out.println("Note / _onBackPressed / view all mode");
        stopAV();
        finish();
    }
}

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  ww.  j  a v a  2  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:de.timroes.swipetodismiss.SwipeDismissList.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }//ww  w .  j  av  a2s  .co  m

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused || !mEnabled) {
            return false;
        }
        resetSwipeState();

        // TODO: ensure this is a finger, and set a flag

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mListView.getChildCount();
        int[] listViewCoords = new int[2];
        mListView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                if (mSwipeLayout != 0) {
                    View swipeView = child.findViewById(mSwipeLayout);
                    if (swipeView != null) {
                        mDownView = swipeView;
                        break;
                    }
                }
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mListView.getPositionForView(mDownView);

            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX
                && mSwiping && isDirectionValid(mVelocityTracker.getXVelocity())
                && deltaX >= mViewWidth * 0.2f) {
            dismiss = true;
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mPaused = true;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        resetSwipeState();
        break;
    }

    case MotionEvent.ACTION_MOVE: {

        if (mUndoPopup.isShowing()) {
            // Send a delayed message to hide popup
            mHandler.sendMessageDelayed(mHandler.obtainMessage(mDelayedMsgId), mAutoHideDelay);
        }

        if (mVelocityTracker == null || mPaused || !mEnabled) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        // Only start swipe in correct direction
        if (isDirectionValid(deltaX)) {
            if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {
                mSwiping = true;
                mListView.requestDisallowInterceptTouchEvent(true);

                // Cancel ListView's touch (un-highlighting the item)
                MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                        .getActionIndex(motionEvent) << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                mListView.onTouchEvent(cancelEvent);
                cancelEvent.recycle();
            }
        } else {
            // If we swiped into wrong direction, act like this was the new
            // touch down point
            mDownX = motionEvent.getRawX();
            deltaX = 0;
        }

        if (mSwiping) {
            setTranslationX(mDownView, deltaX);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:android.support.v7.widget.SwitchCompat.java

private void cancelSuperTouch(MotionEvent ev) {
    MotionEvent cancel = MotionEvent.obtain(ev);
    cancel.setAction(MotionEvent.ACTION_CANCEL);
    super.onTouchEvent(cancel);
    cancel.recycle();
}

From source file:android.support.wear.widget.util.ArcSwipe.java

private Swiper.Status sendArcSwipe(UiController uiController, float[] startCoordinates, float[] endCoordinates,
        float[] precision, int duration, boolean isClockwise) {

    float[][] steps = interpolate(startCoordinates, endCoordinates, SWIPE_EVENT_COUNT, isClockwise);
    final int delayBetweenMovements = duration / steps.length;

    MotionEvent downEvent = MotionEvents.sendDown(uiController, startCoordinates, precision).down;
    try {/*  w w w  . j  a  v a2s .c o  m*/
        for (int i = 0; i < steps.length; i++) {
            if (!MotionEvents.sendMovement(uiController, downEvent, steps[i])) {
                Log.e(TAG, "Injection of move event as part of the swipe failed. Sending cancel " + "event.");
                MotionEvents.sendCancel(uiController, downEvent);
                return Swiper.Status.FAILURE;
            }

            long desiredTime = downEvent.getDownTime() + delayBetweenMovements * i;
            long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
            if (timeUntilDesired > 10) {
                uiController.loopMainThreadForAtLeast(timeUntilDesired);
            }
        }

        if (!MotionEvents.sendUp(uiController, downEvent, endCoordinates)) {
            Log.e(TAG, "Injection of up event as part of the swipe failed. Sending cancel event.");
            MotionEvents.sendCancel(uiController, downEvent);
            return Swiper.Status.FAILURE;
        }
    } finally {
        downEvent.recycle();
    }
    return Swiper.Status.SUCCESS;
}

From source file:org.catrobat.catroid.uitest.util.UiTestUtils.java

public static void longClickAndDrag(final Solo solo, final float xFrom, final float yFrom, final float xTo,
        final float yTo, final int steps) {
    final Activity activity = solo.getCurrentActivity();
    Handler handler = new Handler(activity.getMainLooper());

    handler.post(new Runnable() {

        public void run() {
            MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN, xFrom, yFrom, 0);
            activity.dispatchTouchEvent(downEvent);
            downEvent.recycle();
        }/*from   w  ww .j a v a 2  s. co m*/
    });

    solo.sleep(ViewConfiguration.getLongPressTimeout() + 200);

    handler.post(new Runnable() {
        public void run() {
            double offsetX = xTo - xFrom;
            offsetX /= steps;
            double offsetY = yTo - yFrom;
            offsetY /= steps;
            for (int i = 0; i <= steps; i++) {
                float x = xFrom + (float) (offsetX * i);
                float y = yFrom + (float) (offsetY * i);
                MotionEvent moveEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
                activity.dispatchTouchEvent(moveEvent);

                solo.sleep(20);
                moveEvent.recycle();
            }
        }
    });

    solo.sleep(steps * 20 + 200);

    handler.post(new Runnable() {

        public void run() {
            MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_UP, xTo, yTo, 0);
            activity.dispatchTouchEvent(upEvent);
            upEvent.recycle();
            Log.d(TAG, "longClickAndDrag finished: " + (int) yTo);
        }
    });

    solo.sleep(1000);
}