Example usage for android.view VelocityTracker addMovement

List of usage examples for android.view VelocityTracker addMovement

Introduction

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

Prototype

public void addMovement(MotionEvent event) 

Source Link

Document

Add a user's movement to the tracker.

Usage

From source file:com.oguzbabaoglu.cardpager.CardPager.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our descendants.
        return false;
    }/*from   w ww . j  ava2  s . c o  m*/

    if (pagerAdapter == null || pagerAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

    if (velocityTracker == null) {
        velocityTracker = VelocityTracker.obtain();
    }
    velocityTracker.addMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:

        // Do not interfere with the settling action.
        if (scrollState != SCROLL_STATE_SETTLING) {
            scroller.abortAnimation();
            populatePending = false;
            populate();
        }

        // Remember where the motion event started
        lastMotionX = initialMotionX = ev.getX();
        lastMotionY = initialMotionY = ev.getY();
        activePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;

    case MotionEvent.ACTION_MOVE:
        if (!dragInProgress) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - lastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - lastMotionY);

            if (xDiff > touchSlop && xDiff > yDiff) {

                dragInProgress = true;
                lastMotionX = x - initialMotionX > 0 ? initialMotionX + touchSlop : initialMotionX - touchSlop;
                lastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);

                // Disallow Parent Intercept, just in case
                requestParentDisallowInterceptTouchEvent(true);
            }
        }
        // Not else! Note that dragInProgress can be set above.
        if (dragInProgress) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            performDrag(x);
        }
        break;

    case MotionEvent.ACTION_UP:

        if (!dragInProgress) {
            break;
        }

        final VelocityTracker velocityTracker = this.velocityTracker;
        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        final int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerId);

        populatePending = true;

        final int width = getClientWidth();
        final int scrollX = getScrollX();
        final ItemInfo ii = infoForCurrentScrollPosition();
        final int currentPage = ii.position;
        final float pageOffset = ((float) scrollX / width) - ii.offset;
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final int totalDelta = (int) (x - initialMotionX);
        final int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);

        setCurrentItemInternal(nextPage, true, true, initialVelocity);
        activePointerId = INVALID_POINTER;
        endDrag();
        break;

    case MotionEvent.ACTION_CANCEL:

        if (!dragInProgress) {
            break;
        }

        scrollToItem(currentItem, true, 0);
        activePointerId = INVALID_POINTER;
        endDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN:
        final int index = MotionEventCompat.getActionIndex(ev);
        lastMotionX = MotionEventCompat.getX(ev, index);
        activePointerId = MotionEventCompat.getPointerId(ev, index);
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        lastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, activePointerId));
        break;
    }

    return true;
}