Example usage for android.view VelocityTracker obtain

List of usage examples for android.view VelocityTracker obtain

Introduction

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

Prototype

static public VelocityTracker obtain() 

Source Link

Document

Retrieve a new VelocityTracker object to watch the velocity of a motion.

Usage

From source file:com.matcontrol.control.BottomSheetBehavior_v25.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        mIgnoreEvents = true;/*from  w  w w  .jav a2 s .  c o m*/
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();

        if (mNestedScrollingChildRefList != null) {
            for (View childView : mNestedScrollingChildRefList) {
                if (childView != null && parent.isPointInChildBounds(childView, initialX, mInitialY)) {
                    mActivePointerId = event.getPointerId(event.getActionIndex());
                    mTouchingScrollingChild = true;
                }
            }
        }

        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    return action == MotionEvent.ACTION_MOVE && !mIgnoreEvents && mState != STATE_DRAGGING
            && !isPointInsideChildScrollView(parent, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:android.support.design.widget.SheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//  w  w w.  j a  v a2s.c o m
    int action = MotionEventCompat.getActionMasked(event);
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE) {

        if (mSlideHelper.canScrollHorizontally() != 0
                && Math.abs(mInitialY - event.getX()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        } else if (mSlideHelper.canScrollVertically() != 0
                && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return true;
}

From source file:com.hardsoftstudio.anchorbottomsheet.AnchorSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }/*from   w  w  w .  ja v  a2  s  .co m*/
    int action = MotionEventCompat.getActionMasked(event);
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return true;
}

From source file:vc908.stickerfactory.ui.advancedrecyclerview.swipeable.RecyclerViewSwipeManager.java

/**
 * Constructor./*from  w w w  .  j  av  a2s.  c o m*/
 */
public RecyclerViewSwipeManager() {
    mInternalUseOnItemTouchListener = new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return RecyclerViewSwipeManager.this.onInterceptTouchEvent(rv, e);
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            RecyclerViewSwipeManager.this.onTouchEvent(rv, e);
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean arg0) {
            // TODO Auto-generated method stub

        }
    };
    mItemSlideAnimator = new ItemSlidingAnimator();
    mVelocityTracker = VelocityTracker.obtain();
}

From source file:com.yj.ecard.ui.views.viewflow.ViewFlow.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }// w w  w .  ja v a 2s. c om
    mVelocityTracker.addMovement(ev);

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        // ??
        if (handler != null)
            handler.removeMessages(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);

        boolean xMoved = xDiff > mTouchSlop;

        if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - getWidth();
                if (availableToScroll > 0) {
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                }
            }
            return true;
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            }
            // else if (velocityX < -SNAP_VELOCITY
            // && mCurrentScreen == getChildCount() - 1) {
            // snapToScreen(0);
            // }
            // else if (velocityX > SNAP_VELOCITY
            // && mCurrentScreen == 0) {
            // snapToScreen(getChildCount() - 1);
            // }
            else {
                snapToDestination();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        // action_up?????
        if (handler != null) {
            Message message = handler.obtainMessage(0);
            handler.sendMessageDelayed(message, timeSpan);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        snapToDestination();
        mTouchState = TOUCH_STATE_REST;
    }
    return true;
}

From source file:android.support.design.widget.BottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//from ww  w .  j  a v  a 2 s .  c om
    int action = MotionEventCompat.getActionMasked(event);
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

From source file:net.osmand.plus.widgets.tools.ExtendedBottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//w w  w  .  j  a v  a2 s.co m
    int action = event.getActionMasked();
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

From source file:com.commonsware.cwac.crossport.design.widget.BottomSheetBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }//from  w  w w  . j a v  a 2 s . c  o  m
    int action = event.getActionMasked();
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    if (mViewDragHelper != null) {
        mViewDragHelper.processTouchEvent(event);
    }
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
        if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return !mIgnoreEvents;
}

From source file:com.lovely3x.eavlibrary.EasyAdapterView.java

public EasyAdapterView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    this.mDebugPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.mDebugPaint.setColor(Color.RED);
    this.mDebugPaint.setStrokeWidth(5);
    this.mDebugPaint.setTextSize(
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));

    ViewConfiguration config = ViewConfiguration.get(context);
    mTouchSlop = config.getScaledTouchSlop() / 2;
    this.mVelocity = VelocityTracker.obtain();

    mMinimumVelocity = config.getScaledMinimumFlingVelocity();
    mMaximumVelocity = config.getScaledMaximumFlingVelocity();

    this.mTopEdgeEffect = new EdgeEffect(getContext());
    this.mBottomEdgeEffect = new EdgeEffect(getContext());

    this.mLeftEdgeEffect = new EdgeEffect(getContext());
    this.mRightEdgeEffect = new EdgeEffect(getContext());

    mDividerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f,
            getResources().getDisplayMetrics());
    mDivider = new ColorDrawable(Color.LTGRAY);

    if (attrs != null)
        initAttrs(attrs);//w w  w.  j a v a 2s  . c  o m

    setWillNotDraw(false);

    previewInEditMode();
}

From source file:com.trafi.anchorbottomsheetbehavior.AnchorBottomSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown() || !mAllowUserDragging) {
        mIgnoreEvents = true;/*from   w w w  .  j a v  a  2 s.  c  o  m*/
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();
        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }
        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}