Example usage for android.view MotionEvent getActionMasked

List of usage examples for android.view MotionEvent getActionMasked

Introduction

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

Prototype

public final int getActionMasked() 

Source Link

Document

Return the masked action being performed, without pointer index information.

Usage

From source file:org.mozilla.gecko.home.HomePager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        // Drop the soft keyboard by stealing focus from the URL bar.
        requestFocus();//from  w  w  w .  j  a  v  a2s  .c om
    }

    return super.onInterceptTouchEvent(event);
}

From source file:ti.modules.titanium.ui.widget.listview.TiNestedListView.java

/**
 * Called before onTouchEvent() gets called in this view or a child view,
 * but only while requestDisallowInterceptTouchEvent() is set to false.
 * <p>//www  . j av a2  s  . co m
 * Provides this view the opportunity to monitor or steal child touch events.
 * If this view returns true, then the event will not be received by the child.
 * @param event The touch information received.
 * @return Returns true if this view will claim the event. Returns false if not.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // If touch scrolling is disabled, then block touch move/drag events.
    if (!this.isTouchScrollable && (event.getActionMasked() == MotionEvent.ACTION_MOVE)) {
        return false;
    }

    // Let the base class handle the touch event.
    return super.onInterceptTouchEvent(event);
}

From source file:ti.modules.titanium.ui.widget.listview.TiNestedListView.java

/**
 * Called when a touch event has been received by this view.
 * @param event The touch information received.
 * @return Returns true if this view handled the touch. Returns false if not.
 *///from  ww  w . j  a v a  2s  .com
@Override
public boolean onTouchEvent(MotionEvent event) {
    // If touch scrolling is disabled, then block touch move/drag events.
    if (!this.isTouchScrollable && (event.getActionMasked() == MotionEvent.ACTION_MOVE)) {
        return false;
    }

    // Let the base class handle the touch event.
    return super.onTouchEvent(event);
}

From source file:com.google.android.apps.muzei.util.PanScaleProxyView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        mMotionEventDown = true;/*from   www .j a v a2s  .com*/
        mNonSingleTapGesture = false;
        mNonSingleTapZoomedOut = false;
        if (mOnOtherGestureListener != null) {
            mOnOtherGestureListener.onDown();
        }
    }
    boolean retVal = mScaleGestureDetector.onTouchEvent(event);
    retVal = mGestureDetector.onTouchEvent(event) || retVal;
    if (mMotionEventDown && event.getActionMasked() == MotionEvent.ACTION_UP) {
        mMotionEventDown = false;
        if (mNonSingleTapGesture && mOnOtherGestureListener != null) {
            mOnOtherGestureListener.onUpNonSingleTap(mNonSingleTapZoomedOut);
        }
    }
    return retVal || super.onTouchEvent(event);
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

@Override
public boolean onInterceptTouchEvent(final RecyclerView view, MotionEvent motionEvent) {
    if (mPaused)/*from ww w .ja  v  a  2s .c  om*/
        return false;
    // offset because the view is translated during swipe
    motionEvent.offsetLocation(mTranslationX, 0);

    if (mViewWidth < 2) {
        mViewWidth = view.getWidth();
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        return down(motionEvent);
    }
    case MotionEvent.ACTION_MOVE: {
        return move(motionEvent);

    }

    }
    return false;
}

From source file:com.nononsenseapps.feeder.ui.SwipeDismissTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // offset because the view is translated during swipe
    motionEvent.offsetLocation(mTranslationX, 0);

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();//from  www. j a v a  2  s  .  c  o m
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        // TODO: ensure this is a finger, and set a flag
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        if (mCallbacks.canDismiss(mToken)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        return false;
    }

    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 && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            mSwipingView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth)
                    //.alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss();
                        }
                    });
        } else if (mSwiping) {
            // cancel
            mSwipingView.animate().translationX(0)
                    //.alpha(1)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mCallbacks.onSwipeCancelled();
                        }
                    });
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

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

        mSwipingView.animate().translationX(0)
                //.alpha(1)
                .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mCallbacks.onSwipeCancelled();
                    }
                });
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null) {
            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);
            mView.getParent().requestDisallowInterceptTouchEvent(true);

            // Cancel listview's touch
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            mView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            if (notNotifiedSwipeStart) {
                notNotifiedSwipeStart = false;
                mCallbacks.onSwipeStarted(deltaX > 0);
            }
            mTranslationX = deltaX;
            mSwipingView.setTranslationX(deltaX - mSwipingSlop);
            //mView.setAlpha(mInterpolator.getInterpolation(1f - 1f * Math.abs(deltaX) / mViewWidth));
            //                    mView.setAlpha(Math.max(0f, Math.min(1f,
            //                            1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:es.ugr.swad.swadroid.gui.SwipeListViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }/*from w ww  . ja va 2 s  .c  om*/

    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 = 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)) {
                mDownView = child;
                break;
            }
        }

        if ((mDownView != null) && (mListView != null)) {
            mDownX = motionEvent.getRawX();
            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(500); // 1000 by defaut but it was too much
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean swipe = false;
        boolean swipeRight = false;

        if (Math.abs(deltaX) > mViewWidth / 2) {
            swipe = true;
            swipeRight = deltaX > 0;
        } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
            swipe = true;
            swipeRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (swipe) {
            // sufficient swipe value
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            final boolean toTheRight = swipeRight;
            ++mDismissAnimationRefCount;
            mDownView.animate().translationX(swipeRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performSwipeAction(downView, downPosition, toTheRight,
                                    toTheRight ? dismissRight : dismissLeft);
                        }
                    });
        } else {
            // cancel
            mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker = null;
        mDownX = 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;
        if (Math.abs(deltaX) > mSlop) {
            mSwiping = true;
            mListView.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));
            mListView.onTouchEvent(cancelEvent);
        }

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

From source file:com.layer_net.stepindicator.StepIndicator.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    int pointX = startX;
    int xTouch;/*from www .  j  a v a2s. c  om*/
    int yTouch;
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        xTouch = (int) event.getX(0);
        yTouch = (int) event.getY(0);
        for (int i = 0; i < stepsCount; i++) {
            if (Math.abs(xTouch - pointX) < radius + 5 && Math.abs(yTouch - centerY) < radius + 5) {
                if (!withViewpager) {
                    setCurrentStepPosition(i);
                }

                if (onClickListener != null) {
                    onClickListener.onClick(i);
                }
            }
            pointX = pointX + stepDistance;
        }
        break;
    }
    return super.onTouchEvent(event);
}

From source file:org.mozilla.gecko.ui.PanZoomController.java

public boolean onTouchEvent(MotionEvent event) {
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        return onTouchStart(event);
    case MotionEvent.ACTION_MOVE:
        return onTouchMove(event);
    case MotionEvent.ACTION_UP:
        return onTouchEnd(event);
    case MotionEvent.ACTION_CANCEL:
        return onTouchCancel(event);
    default:/*from  w w  w . j a v  a2 s  .c om*/
        return false;
    }
}

From source file:se.kth.csc.stayawhile.swipe.QueueTouchListener.java

private boolean handleTouchEvent(MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }/*  w  ww.ja va 2  s  .  c o  m*/

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

        // 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 && mAnimatingPosition != mRecyclerView.getChildLayoutPosition(mDownView)) {
            mAlpha = ViewCompat.getAlpha(mDownView);
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView);
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        break;
    }

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

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

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

        mFinalDelta = 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(mFinalDelta) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = mFinalDelta > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (mFinalDelta < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) {
            final int downPosition = mDownPosition;
            if (dismissRight) {
                ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                        .setListener(new ViewPropertyAnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(View view) {
                                mQueueSwipeListener.onSetHelp(mRecyclerView, new int[] { downPosition });
                            }
                        });
            } else {
                final View downView = mDownView; // mDownView gets null'd before animation ends
                ++mDismissAnimationRefCount;
                mAnimatingPosition = mDownPosition;
                ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                        .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListenerAdapter() {

                            @Override
                            public void onAnimationEnd(View view) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                    performDismiss(downView, downPosition);
                                }
                            }
                        });
            }
        } else {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).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 (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        }

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

    return false;
}