Example usage for android.view MotionEvent getRawX

List of usage examples for android.view MotionEvent getRawX

Introduction

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

Prototype

public final float getRawX() 

Source Link

Document

Returns the original raw X coordinate of this event.

Usage

From source file:com.tmall.wireless.tangram3.view.BannerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    float x = ev.getRawX();
    float y = ev.getRawY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        xDown = x;//w  ww  . j  av a2 s. c  om
        yDown = y;
        break;
    case MotionEvent.ACTION_MOVE:
        int xDiff = (int) (x - xDown);
        int yDiff = (int) (y - yDown);

        direction = -xDiff;

        if (Math.abs(xDiff) >= Math.abs(yDiff)) {
            getParent().requestDisallowInterceptTouchEvent(true);
        } else {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        break;
    case MotionEvent.ACTION_UP:
        direction = 1;
        break;
    default:
        break;
    }

    return false;
}

From source file:com.onyx.deskclock.deskclock.alarms.AlarmActivity.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mAlarmHandled) {
        LogUtils.v(LOGTAG, "onTouch ignored: %s", motionEvent);
        return false;
    }/*from  ww  w .j a  va  2  s  . co m*/

    final int[] contentLocation = { 0, 0 };
    mContentView.getLocationOnScreen(contentLocation);

    final float x = motionEvent.getRawX() - contentLocation[0];
    final float y = motionEvent.getRawY() - contentLocation[1];

    final int alarmLeft = mAlarmButton.getLeft() + mAlarmButton.getPaddingLeft();
    final int alarmRight = mAlarmButton.getRight() - mAlarmButton.getPaddingRight();

    final float snoozeFraction, dismissFraction;

    if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        snoozeFraction = getFraction(alarmRight, mSnoozeButton.getLeft(), x);
        dismissFraction = getFraction(alarmLeft, mDismissButton.getRight(), x);
    } else {
        snoozeFraction = getFraction(alarmLeft, mSnoozeButton.getRight(), x);
        dismissFraction = getFraction(alarmRight, mDismissButton.getLeft(), x);
    }

    // API >= 17
    /*if (mContentView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
    snoozeFraction = getFraction(alarmRight, mSnoozeButton.getLeft(), x);
    dismissFraction = getFraction(alarmLeft, mDismissButton.getRight(), x);
    } else {
    snoozeFraction = getFraction(alarmLeft, mSnoozeButton.getRight(), x);
    dismissFraction = getFraction(alarmRight, mDismissButton.getLeft(), x);
    }*/

    setAnimatedFractions(snoozeFraction, dismissFraction);

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        LogUtils.v(LOGTAG, "onTouch started: %s", motionEvent);

        // Stop the pulse, allowing the last pulse to finish.
        mPulseAnimator.setRepeatCount(0);
        break;
    case MotionEvent.ACTION_UP:
        LogUtils.v(LOGTAG, "onTouch ended: %s", motionEvent);

        if (snoozeFraction == 1.0f) {
            snooze();
        } else if (dismissFraction == 1.0f) {
            dismiss();
        } else {
            if (snoozeFraction > 0.0f || dismissFraction > 0.0f) {
                // Animate back to the initial state.
                AnimatorUtils.reverse(mAlarmAnimator, mSnoozeAnimator, mDismissAnimator);
            } else if (mAlarmButton.getTop() <= y && y <= mAlarmButton.getBottom()) {
                // User touched the alarm button, hint the dismiss action
                hintDismiss();
            }

            // Restart the pulse.
            mPulseAnimator.setRepeatCount(ValueAnimator.INFINITE);
            if (!mPulseAnimator.isStarted()) {
                mPulseAnimator.start();
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        resetAnimations();
        break;
    default:
        break;
    }

    return true;
}

From source file:demo.camera.library.ui.CameraCaptureActivity.java

public void setUpUi() {
    mBlockerSpinner = (RelativeLayout) findViewById(R.id.blocker);
    mBlockerSpinner.setVisibility(View.GONE);

    mTouchIndicator = (ImageView) findViewById(R.id.touchIndicator);
    mTouchInterceptor = (RelativeLayout) findViewById(R.id.touch_interceptor);
    mTouchInterceptor.setOnTouchListener(new View.OnTouchListener() {
        @Override//from ww w.j a va  2s  . co  m
        public boolean onTouch(View v, MotionEvent event) {
            mTouchIndicator.setImageResource(R.drawable.white_circle);
            mTouchIndicator.setVisibility(View.VISIBLE);
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();

            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) mTouchIndicator
                    .getLayoutParams();
            lParams.leftMargin = X - mTouchIndicator.getWidth() / 2;
            lParams.topMargin = Y - mTouchIndicator.getHeight() / 2;
            mTouchIndicator.setLayoutParams(lParams);
            mTouchIndicator.invalidate();

            ScaleAnimation scaleUpAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF,
                    (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);

            scaleUpAnimation.setDuration(350);
            scaleUpAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mTouchIndicator.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mTouchIndicator.setVisibility(View.GONE);
                        }
                    }, 100);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            mTouchIndicator.startAnimation(scaleUpAnimation);
            return false;
        }
    });

    //            mTouchInterceptor.setVisibility(View.GONE);

    mRecordButton = (Button) findViewById(R.id.recordButton);

    mExtrasContainer = (LinearLayout) findViewById(R.id.settings_container);
    mMoreOptions = (ImageView) findViewById(R.id.icon_more);
    mMoreOptions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mMoreOptions.setSelected(!mMoreOptions.isSelected());
            if (mMoreOptions.isSelected()) {
                mExtrasContainer.setVisibility(View.VISIBLE);
            } else {
                mExtrasContainer.setVisibility(View.GONE);
            }

        }
    });

    //            mRecordButton .setOnClickListener(mRecordButtonClickListener);
    setUpTouchInterceptor(mRecordButton);

    setUpHeaders();
    setUpFlashButton();
    setUpProgressIndicator();

    //        setupFilterSpinner();
    setupCameraFlipper();
}

From source file:de.Maxr1998.xposed.maxlock.ui.LockFragment.java

@SuppressWarnings("deprecation")
private void setupKnockCodeLayout() {
    final View container = rootView.findViewById(R.id.container);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) container.getLayoutParams();
    params.setMargins(0, 0, 0, 0);//  w  w  w.  j a  va  2 s. c  o m
    container.setLayoutParams(params);
    container.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
                mInputText.append("\u2022");

                // Center values
                int[] loc = new int[2];
                container.getLocationOnScreen(loc);
                int viewCenterX = loc[0] + container.getWidth() / 2;
                int viewCenterY = loc[1] + container.getHeight() / 2;

                // Track touch positions
                knockCodeX.add(e.getRawX());
                knockCodeY.add(e.getRawY());
                if (knockCodeX.size() != knockCodeY.size()) {
                    throw new RuntimeException("The amount of the X and Y coordinates doesn't match!");
                }

                // Calculate center
                float centerX;
                float differenceX = Collections.max(knockCodeX) - Collections.min(knockCodeX);
                if (differenceX > 50) {
                    centerX = Collections.min(knockCodeX) + differenceX / 2;
                } else
                    centerX = viewCenterX;

                float centerY;
                float differenceY = Collections.max(knockCodeY) - Collections.min(knockCodeY);
                if (differenceY > 50) {
                    centerY = Collections.min(knockCodeY) + differenceY / 2;
                } else
                    centerY = viewCenterY;

                // Calculate key
                key.setLength(0);
                for (int i = 0; i < knockCodeX.size(); i++) {
                    float x = knockCodeX.get(i), y = knockCodeY.get(i);
                    if (x < centerX && y < centerY)
                        key.append("1");
                    else if (x > centerX && y < centerY)
                        key.append("2");
                    else if (x < centerX && y > centerY)
                        key.append("3");
                    else if (x > centerX && y > centerY)
                        key.append("4");
                }
                checkInput();
                return true;
            }
            return false;
        }
    });
    divider = new View(getActivity());
    divider.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            Math.round(getResources().getDisplayMetrics().density)));
    divider.setBackgroundColor(getResources().getColor(R.color.light_white));
    ((ViewGroup) container).addView(divider);
    if (prefs.getBoolean(Common.INVERT_COLOR, false) && prefs.getBoolean(Common.KC_SHOW_DIVIDERS, true)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            divider.setBackground(getResources().getDrawable(android.R.color.black));
        else
            divider.setBackgroundDrawable(getResources().getDrawable(android.R.color.black));
    } else if (!prefs.getBoolean(Common.KC_SHOW_DIVIDERS, true) || screenWidth > screenHeight) {
        divider.setVisibility(View.GONE);
    }
}

From source file:de.mrapp.android.bottomsheet.view.DraggableView.java

@Override
public final boolean dispatchTouchEvent(final MotionEvent event) {
    boolean handled = false;

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        break;//from  w w w . j ava 2 s  .c om
    case MotionEvent.ACTION_MOVE:
        dragHelper.update(event.getRawY());

        if (isMaximized() && (event.getRawY() - dragHelper.getStartPosition() < 0
                || isScrollUpEvent(event.getRawX(), event.getRawY()))) {
            dragHelper.reset();
            break;
        }

        handled = handleDrag();
        break;
    case MotionEvent.ACTION_UP:
        dragHelper.reset();

        if (dragHelper.hasThresholdBeenReached()) {
            handleRelease();
        }

        break;
    default:
        break;
    }

    return handled || super.dispatchTouchEvent(event);
}

From source file:com.sj.android.appusage.ui.widgets.listview.SwipeListViewTouchListener.java

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

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }/*w ww.j  av  a 2s.  c  om*/

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.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 = swipeListView.getChildAt(i);
            child.getHitRect(rect);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setFrontView(child.findViewById(swipeFrontView), childPosition);
                currentChild = child;
                downX = motionEvent.getRawX();
                downY = motionEvent.getRawY();
                downPosition = childPosition;

                frontView.setClickable(false);
                frontView.setLongClickable(false);

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) {
            break;
        }
        float deltaX = motionEvent.getRawX() - downX;
        float deltaY = motionEvent.getRawY() - downY;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (currentChild == null) {
            break;
        }

        if (Math.abs(deltaX) > currentChild.getWidth() / 2
                && (Math.abs(deltaY) < (currentChild.getHeight() * 3 / 4))) {
            swap = true;
            swapRight = deltaX > 0;
        } else {
            swap = false;
            swapRight = false;
        }

        generateAnimate(frontView, swap, swapRight, downPosition, deltaX);

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        float deltaX = motionEvent.getRawX() - downX;

        if ((deltaX > slop) && swipeCurrentAction == SwipeListView.SWIPE_ACTION_REVEAL
                && velocityY < velocityX) {
            swiping = true;
            swipingRight = (deltaX > 0);
            if (SwipeListView.DEBUG) {
                Log.d(SwipeListView.TAG, "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            }
            swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_REVEAL) {
                backView.setVisibility(View.VISIBLE);
            }
        }
        break;
    }
    }
    return false;
}

From source file:org.odk.collect.android.widgets.ImageWebViewWidget.java

public boolean suppressFlingGesture(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (mImageDisplay == null || mImageDisplay.getVisibility() != View.VISIBLE) {
        return false;
    }//from www .j  a  va 2 s .  c om

    Rect rect = new Rect();
    mImageDisplay.getHitRect(rect);

    // Log.i(t, "hitRect: " + rect.left + "," + rect.top + " : " +
    // rect.right + "," + rect.bottom );
    // Log.i(t, "e1 Raw, Clean: " + e1.getRawX() + "," + e1.getRawY() +
    // " : " + e1.getX() + "," + e1.getY());
    // Log.i(t, "e2 Raw, Clean: " + e2.getRawX() + "," + e2.getRawY() +
    // " : " + e2.getX() + "," + e2.getY());

    // starts in WebView
    if (rect.contains((int) e1.getRawX(), (int) e1.getRawY())) {
        return true;
    }

    // ends in WebView
    if (rect.contains((int) e2.getRawX(), (int) e2.getRawY())) {
        return true;
    }

    // transits WebView
    if (rect.contains((int) ((e1.getRawX() + e2.getRawX()) / 2.0),
            (int) ((e1.getRawY() + e2.getRawY()) / 2.0))) {
        return true;
    }
    // Log.i(t, "NOT SUPPRESSED");
    return false;
}

From source file:cn.meiqu.baseproject.view.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

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

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

        // 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;
    }

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

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

    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());
        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 != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(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 = 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 (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 | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

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

From source file:com.aako.zjp2p.widget.superrecycler.swipe.SwipeDismissRecyclerViewTouchListener.java

@SuppressLint("AndroidLintClickableViewAccessibility")
@Override/*from  w  ww  .  j a v  a  2  s  .  c  om*/
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }

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

        // 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;
    }

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

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

    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());
        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 != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(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 = 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 (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 | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

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

From source file:com.adamin.superrecyclerview.superrecycer.swipe.SwipeDismissRecyclerViewTouchListener.java

@SuppressLint("AndroidLintClickableViewAccessibility")
@Override/*from  w w w.ja  va  2  s . com*/
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }

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

        // 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;
    }

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

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

    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());
        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 != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            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) {
                            super.onAnimationEnd(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 = 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 (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 | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

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