Java Code Examples for android.view.VelocityTracker

The following are top voted examples for showing how to use android.view.VelocityTracker. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: GitHub   File: Utils.java   Source Code and License 6 votes vote down vote up
public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev,
                                                              VelocityTracker tracker) {

    // Check the dot product of current velocities.
    // If the pointer that left was opposing another velocity vector, clear.
    tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
    final int upIndex = ev.getActionIndex();
    final int id1 = ev.getPointerId(upIndex);
    final float x1 = tracker.getXVelocity(id1);
    final float y1 = tracker.getYVelocity(id1);
    for (int i = 0, count = ev.getPointerCount(); i < count; i++) {
        if (i == upIndex)
            continue;

        final int id2 = ev.getPointerId(i);
        final float x = x1 * tracker.getXVelocity(id2);
        final float y = y1 * tracker.getYVelocity(id2);

        final float dot = x + y;
        if (dot < 0) {
            tracker.clear();
            break;
        }
    }
}
 
Example 2
Project: GitHub   File: DirectionalViewpager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    if (isHorizontal()) {
        mInitialMotionX = mLastMotionX = 0;
    } else {
        mInitialMotionY = mLastMotionY = 0;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 3
Project: GitHub   File: VerticalViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <p/>
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * <p/>
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 4
Project: GitHub   File: VerticalViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * End a fake drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
            velocityTracker, mActivePointerId);
    mPopulatePending = true;
    final int height = getClientHeight();
    final int scrollY = getScrollY();
    final ItemInfo ii = infoForCurrentScrollPosition();
    final int currentPage = ii.position;
    final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
    final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 5
Project: GitHub   File: BaseViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <p/>
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 * <p/>
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    if (isHorizontalDirection()) {
        mInitialMotionX = mLastMotionX = 0;
    } else if (isVerticalDirection()) {
        mInitialMotionY = mLastMotionY = 0;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 6
Project: GitHub   File: BaseViewPager.java   Source Code and License 6 votes vote down vote up
private void endFakeDragHorizontally() {
    if (mAdapter != null) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = 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) / ii.widthFactor;
        final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
        int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
                totalDelta);
        setCurrentItemInternal(nextPage, true, true, initialVelocity);
    }
    endDrag();

    mFakeDragging = false;
}
 
Example 7
Project: GitHub   File: BaseViewPager.java   Source Code and License 6 votes vote down vote up
private void endFakeDragVertically() {
    if (mAdapter != null) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(
                velocityTracker, mActivePointerId);
        mPopulatePending = true;
        final int height = getClientHeight();
        final int scrollY = getScrollY();
        final ItemInfo ii = infoForCurrentScrollPosition();
        final int currentPage = ii.position;
        final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
        final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
        int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
                totalDelta);
        setCurrentItemInternal(nextPage, true, true, initialVelocity);
    }
    endDrag();

    mFakeDragging = false;
}
 
Example 8
Project: OpenWithX   File: ResolverDrawerLayout.java   Source Code and License 6 votes vote down vote up
public ResolverDrawerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResolverDrawerLayout,
            defStyleAttr, 0);
    mMaxWidth = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_android_maxWidth, -1);
    mMaxCollapsedHeight = a.getDimensionPixelSize(
            R.styleable.ResolverDrawerLayout_maxCollapsedHeight, 0);
    mMaxCollapsedHeightSmall = a.getDimensionPixelSize(
            R.styleable.ResolverDrawerLayout_maxCollapsedHeightSmall,
            mMaxCollapsedHeight);
    a.recycle();

    mScrollIndicatorDrawable = context.getDrawable(R.drawable.scroll_indicator_material);

    mScroller = new OverScroller(context, AnimationUtils.loadInterpolator(context,
            android.R.interpolator.decelerate_quint));
    mVelocityTracker = VelocityTracker.obtain();

    final ViewConfiguration vc = ViewConfiguration.get(context);
    mTouchSlop = vc.getScaledTouchSlop();
    mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();

    setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
 
Example 9
Project: KUtils-master   File: BGAViewPager.java   Source Code and License 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception e) {
    }
    return xVelocity;
}
 
Example 10
Project: StackCardsView   File: SwipeTouchHelper.java   Source Code and License 6 votes vote down vote up
private void onTouchRelease() {
    final StackCardsView.LayoutParams lp = (StackCardsView.LayoutParams) mTouchChild.getLayoutParams();
    if (lp.fastDismissAllowed) {
        final VelocityTracker velocityTracker2 = mVelocityTracker;
        velocityTracker2.computeCurrentVelocity(1000, mMaxVelocity);
        float xv = velocityTracker2.getXVelocity(mActivePointerId);
        float yv = velocityTracker2.getYVelocity(mActivePointerId);
        if (doFastDisappear(xv, yv)) {
            resetTouch();
            return;
        }
    }
    if (isDistanceAllowDismiss() && isDirectionAllowDismiss()) {
        doSlowDisappear();
    } else {
        animateToInitPos();
    }
    resetTouch();
    mSwipeView.onCoverStatusChanged(isCoverIdle());
}
 
Example 11
Project: PageScrollView   File: PageScrollView.java   Source Code and License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mVirtualCount == 0 || !isEnabled()) {
        return super.onTouchEvent(event);
    }
    if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    final int action = event.getAction() & MotionEventCompat.ACTION_MASK;
    if (action == MotionEvent.ACTION_MOVE) {
        handleTouchActionMove(event);
    } else {
        if (action == MotionEvent.ACTION_DOWN) {
            handleTouchActionDown(event);
        }
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            handleTouchActionUp(event);
        }
    }
    return true;
}
 
Example 12
Project: PureMusic   File: LyricView.java   Source Code and License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_CANCEL:
            actionCancel(event);
            break;
        case MotionEvent.ACTION_DOWN:
            actionDown(event);
            break;
        case MotionEvent.ACTION_MOVE:
            actionMove(event);
            break;
        case MotionEvent.ACTION_UP:
            actionUp(event);
            break;
        default:
            break;
    }
    invalidateView();
    return true;
}
 
Example 13
Project: KUtils   File: BGAViewPager.java   Source Code and License 6 votes vote down vote up
private float getXVelocity() {
    float xVelocity = 0;
    Class viewpagerClass = ViewPager.class;
    try {
        Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
        velocityTrackerField.setAccessible(true);
        VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);

        Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
        activePointerIdField.setAccessible(true);

        Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
        maximumVelocityField.setAccessible(true);
        int maximumVelocity = maximumVelocityField.getInt(this);

        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
    } catch (Exception e) {
    }
    return xVelocity;
}
 
Example 14
Project: Renrentou   File: XCCycleViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 15
Project: Renrentou   File: XCCycleViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * End a fake drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
            velocityTracker, mActivePointerId);
    mPopulatePending = 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) / ii.widthFactor;
    final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 16
Project: VerticalWebView   File: VerticalParentViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 17
Project: VerticalWebView   File: VerticalParentViewPager.java   Source Code and License 6 votes vote down vote up
/**
 * End a fake drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
            velocityTracker, mActivePointerId);
    mPopulatePending = 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) / ii.widthFactor;
    final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 18
Project: android-project-gallery   File: ViewPagerCompat.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 19
Project: TextReader   File: DirectionalViewpager.java   Source Code and License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    if (isHorizontal()) {
        mInitialMotionX = mLastMotionX = 0;
    } else {
        mInitialMotionY = mLastMotionY = 0;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 20
Project: YViewPagerDemo   File: YViewPager.java   Source Code and License 6 votes vote down vote up
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 21
Project: boohee_v5.6   File: ViewPager.java   Source Code and License 6 votes vote down vote up
public void endFakeDrag() {
    if (this.mFakeDragging) {
        if (this.mAdapter != null) {
            VelocityTracker velocityTracker = this.mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, (float) this.mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, this.mActivePointerId);
            this.mPopulatePending = true;
            int width = getClientWidth();
            int scrollX = getScrollX();
            ItemInfo ii = infoForCurrentScrollPosition();
            setCurrentItemInternal(determineTargetPage(ii.position, ((((float) scrollX) / ((float) width)) - ii.offset) / ii.widthFactor, initialVelocity, (int) (this.mLastMotionX - this.mInitialMotionX)), true, true, initialVelocity);
        }
        endDrag();
        this.mFakeDragging = false;
        return;
    }
    throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
}
 
Example 22
Project: android-project-gallery   File: ViewPagerCompat.java   Source Code and License 6 votes vote down vote up
/**
 * End a fake drag of the pager.
 *
 * @see #beginFakeDrag()
 * @see #fakeDragBy(float)
 */
public void endFakeDrag() {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    final VelocityTracker velocityTracker = mVelocityTracker;
    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
            velocityTracker, mActivePointerId);
    mPopulatePending = 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) / ii.widthFactor;
    final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
    int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
            totalDelta);
    setCurrentItemInternal(nextPage, true, true, initialVelocity);
    endDrag();

    mFakeDragging = false;
}
 
Example 23
Project: letv   File: ViewPager.java   Source Code and License 6 votes vote down vote up
public boolean beginFakeDrag() {
    if (this.mIsBeingDragged) {
        return false;
    }
    this.mFakeDragging = true;
    setScrollState(1);
    this.mLastMotionX = 0.0f;
    this.mInitialMotionX = 0.0f;
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    } else {
        this.mVelocityTracker.clear();
    }
    long time = SystemClock.uptimeMillis();
    MotionEvent ev = MotionEvent.obtain(time, time, 0, 0.0f, 0.0f, 0);
    this.mVelocityTracker.addMovement(ev);
    ev.recycle();
    this.mFakeDragBeginTime = time;
    return true;
}
 
Example 24
Project: boohee_v5.6   File: BottomSheetBehavior.java   Source Code and License 6 votes vote down vote up
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);
    if (this.mState == 1 && action == 0) {
        return true;
    }
    this.mViewDragHelper.processTouchEvent(event);
    if (action == 0) {
        reset();
    }
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    }
    this.mVelocityTracker.addMovement(event);
    if (action != 2 || Math.abs(((float) this.mInitialY) - event.getY()) <= ((float) this.mViewDragHelper.getTouchSlop())) {
        return true;
    }
    this.mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
    return true;
}
 
Example 25
Project: OSchina_resources_android   File: BannerView.java   Source Code and License 6 votes vote down vote up
public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setOnClickListener(this);
    mScroller = new Scroller(context);
    mVelocityTracker = VelocityTracker.obtain();
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BannerView);
    mHorizontalOffset = array.getDimension(R.styleable.BannerView_horizontal_offset, 0);
    mVerticalOffset = array.getDimension(R.styleable.BannerView_vertical_offset, 0);
    duration = array.getInt(R.styleable.BannerView_scroll_duration, 500);
    array.recycle();

}
 
Example 26
Project: WeatherStream   File: Confetto.java   Source Code and License 6 votes vote down vote up
public boolean onTouchDown(MotionEvent event) {
    final float x = event.getX();
    final float y= event.getY();

    if (doesLocationIntercept(x, y)) {
        this.touchOverride = true;
        this.overrideX = x;
        this.overrideY = y;
        this.overrideDeltaX = currentX - x;
        this.overrideDeltaY = currentY - y;

        velocityTracker = VelocityTracker.obtain();
        velocityTracker.addMovement(event);

        return true;
    } else {
        return false;
    }
}
 
Example 27
Project: RetroMusicPlayer   File: LyricView.java   Source Code and License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_CANCEL:
            actionCancel(event);
            break;
        case MotionEvent.ACTION_DOWN:
            actionDown(event);
            break;
        case MotionEvent.ACTION_MOVE:
            actionMove(event);
            break;
        case MotionEvent.ACTION_UP:
            actionUp(event);
            break;
        default:
            break;
    }
    invalidateView();
    return true;
}
 
Example 28
Project: ChromeLikeTabSwitcher   File: AbstractTouchEventHandler.java   Source Code and License 5 votes vote down vote up
/**
 * Handles, when a drag gesture has been started.
 *
 * @param event
 *         The motion event, which started the drag gesture, as an instance of the class {@link
 *         MotionEvent}. The motion event may not be null
 */
private void handleDown(@NonNull final MotionEvent event) {
    pointerId = event.getPointerId(0);

    if (velocityTracker == null) {
        velocityTracker = VelocityTracker.obtain();
    } else {
        velocityTracker.clear();
    }

    velocityTracker.addMovement(event);
    onDown(event);
}
 
Example 29
Project: Linphone4Android   File: SlidingDrawer.java   Source Code and License 5 votes vote down vote up
private void prepareTracking(int position) {
	mTracking = true;
	mVelocityTracker = VelocityTracker.obtain();
	boolean opening = !mExpanded;

	if (opening) {
		mAnimatedAcceleration = mMaximumAcceleration;
		mAnimatedVelocity = mMaximumMajorVelocity;
		if (mInvert)
			mAnimationPosition = mTopOffset;
		else
			mAnimationPosition = mBottomOffset
					+ (mVertical ? getHeight() - mHandleHeight : getWidth()
							- mHandleWidth);
		moveHandle((int) mAnimationPosition);
		mAnimating = true;
		mHandler.removeMessages(MSG_ANIMATE);
		long now = SystemClock.uptimeMillis();
		mAnimationLastTime = now;
		mCurrentAnimationTime = now + ANIMATION_FRAME_DURATION;
		mAnimating = true;
	} else {
		if (mAnimating) {
			mAnimating = false;
			mHandler.removeMessages(MSG_ANIMATE);
		}
		moveHandle(position);
	}
}
 
Example 30
Project: LaunchEnr   File: SwipeHelper.java   Source Code and License 5 votes vote down vote up
SwipeHelper(int swipeDirection, Callback callback, Context context) {
    mCallback = callback;
    mHandler = new Handler();
    mSwipeDirection = swipeDirection;
    mVelocityTracker = VelocityTracker.obtain();
    mDensityScale =  context.getResources().getDisplayMetrics().density;
    mPagingTouchSlop = ViewConfiguration.get(context).getScaledPagingTouchSlop();

    mLongPressTimeout = (long) (ViewConfiguration.getLongPressTimeout() * 1.5f); // extra long-press!
    mFalsingThreshold = context.getResources().getDimensionPixelSize(
            R.dimen.swipe_helper_falsing_threshold);
    mFlingAnimationUtils = new FlingAnimationUtils(context, getMaxEscapeAnimDuration() / 1000f);
}
 
Example 31
Project: AppFirCloud   File: SwipeMenuLayout.java   Source Code and License 5 votes vote down vote up
/**
 * @param event 向VelocityTracker添加MotionEvent
 * @see VelocityTracker#obtain()
 * @see VelocityTracker#addMovement(MotionEvent)
 */
private void acquireVelocityTracker(final MotionEvent event) {
    if (null == mVelocityTracker) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
}
 
Example 32
Project: boohee_v5.6   File: BooheeRulerView.java   Source Code and License 5 votes vote down vote up
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    float xPosition = event.getX();
    if (this.velocityTracker == null) {
        this.velocityTracker = VelocityTracker.obtain();
    }
    this.velocityTracker.addMovement(event);
    switch (action) {
        case 0:
            this.isActionUp = false;
            this.scroller.forceFinished(true);
            if (this.animator != null) {
                this.animator.cancel();
                break;
            }
            break;
        case 1:
        case 3:
            this.isActionUp = true;
            this.f = true;
            countVelocityTracker(event);
            return false;
        case 2:
            this.isActionUp = false;
            float off = xPosition - this.mLastX;
            if ((this.moveX > this.maxRightOffset || off >= 0.0f) && (this.moveX < this
                    .maxLeftOffset || off <= 0.0f)) {
                this.moveX += off;
                postInvalidate();
                break;
            }
    }
    this.mLastX = xPosition;
    return true;
}
 
Example 33
Project: android_ui   File: VerticalPullHelper.java   Source Code and License 5 votes vote down vote up
/**
 */
@Override
float computeOverScroll(int scrollX, int scrollY, boolean clampedX, boolean clampedY, VelocityTracker tracker, int units) {
	if (scrollY != 0 && clampedY) {
		return tracker.getYVelocity() / (units * 0.05f);
	}
	return 0;
}
 
Example 34
Project: OSchina_resources_android   File: BannerView.java   Source Code and License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    isTouch = true;
    if (event.getPointerCount() >= 2) return false;
    if (mAdapter == null) return super.onTouchEvent(event);
    int count = mAdapter.getCount();
    if (count <= 1) return super.onTouchEvent(event);
    float x = event.getRawX();
    mVelocityTracker.addMovement(event);
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (!mScroller.isFinished()) { // 如果上次的调用没有执行完就取消。
                return false;
            }
            mLastX = x;
            return true;
        case MotionEvent.ACTION_MOVE:
            requestDisallowInterceptTouchEvent(true);
            int dxMove = (int) (mLastX - x);
            mTotalDx += dxMove;
            onScrollBy(dxMove);
            mLastX = x;
            return true;
        case MotionEvent.ACTION_UP:
            isTouch = false;
            if (Math.abs(mTotalDx) <= 10)
                performClick();
        case MotionEvent.ACTION_CANCEL: {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            mXVelocity = velocityTracker.getXVelocity();
            isTouch = false;
            scrollToItem();
            scrollFinish(count);
            break;
        }
    }
    return super.onTouchEvent(event);
}
 
Example 35
Project: EasyReader   File: HorizontalScrollView.java   Source Code and License 5 votes vote down vote up
private void init() {
    WindowManager wm = (WindowManager) getContext()
            .getSystemService(Context.WINDOW_SERVICE);
    windowWidth = wm.getDefaultDisplay().getWidth();//获取屏幕宽度
    halfWindowWidth = windowWidth / 2;
    oneThirdWindowWidth = windowWidth / visualCount;

    mVelocityTracker = VelocityTracker.obtain();
}
 
Example 36
Project: FabulousFilter   File: ViewPagerBottomSheetBehavior.java   Source Code and License 5 votes vote down vote up
@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }
    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;
}
 
Example 37
Project: RX_Demo   File: SwipeMenuLayout.java   Source Code and License 5 votes vote down vote up
/**
 * @param event 向VelocityTracker添加MotionEvent
 * @see VelocityTracker#obtain()
 * @see VelocityTracker#addMovement(MotionEvent)
 */
private void acquireVelocityTracker(final MotionEvent event) {
    if (null == mVelocityTracker) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
}
 
Example 38
Project: MyLife   File: ElasticDrawer.java   Source Code and License 5 votes vote down vote up
protected float getXVelocity(VelocityTracker velocityTracker) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return velocityTracker.getXVelocity(mActivePointerId);
    }

    return velocityTracker.getXVelocity();
}
 
Example 39
Project: letv   File: NestedScrollView.java   Source Code and License 5 votes vote down vote up
private void initOrResetVelocityTracker() {
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    } else {
        this.mVelocityTracker.clear();
    }
}
 
Example 40
Project: Hotspot-master-devp   File: CoverFlowView.java   Source Code and License 5 votes vote down vote up
private void touchBegan(MotionEvent event) {
        endAnimation();
        float x = event.getX();
        mTouchStartX = x;
        mTouchStartY = event.getY();
        mStartTime = AnimationUtils.currentAnimationTimeMillis();
        mStartOffset = mOffset;

        mTouchMoved = false;

        mTouchStartPos = (mTouchStartY / mHeight) * MOVE_POS_MULTIPLE - 5;
        mTouchStartPos /= 2;

        mVelocity = VelocityTracker.obtain();
        mVelocity.addMovement(event);
//        float x = event.getX();
//        mTouchStartX = x;
//        mTouchStartY = event.getY();
//        mStartTime = AnimationUtils.currentAnimationTimeMillis();
//        mStartOffset = mOffset;
//
//        mTouchMoved = false;
//
//        mTouchStartPos = (x / mWidth) * MOVE_POS_MULTIPLE - 5;
//        mTouchStartPos /= 2;
//
//        mVelocity = VelocityTracker.obtain();
//        mVelocity.addMovement(event);
    }
 
Example 41
Project: CustomAndroidOneSheeld   File: MultiDirectionSlidingDrawer.java   Source Code and License 5 votes vote down vote up
private void prepareTracking(int position) {
    mTracking = true;
    mVelocityTracker = VelocityTracker.obtain();
    boolean opening = !mExpanded;

    if (opening) {
        mAnimatedAcceleration = mMaximumAcceleration;
        mAnimatedVelocity = mMaximumMajorVelocity;
        if (mInvert)
            mAnimationPosition = mTopOffset;
        else
            mAnimationPosition = mBottomOffset
                    + (mVertical ? getHeight() - mHandleHeight : getWidth()
                    - mHandleWidth);
        moveHandle((int) mAnimationPosition);
        mAnimating = true;
        mHandler.removeMessages(MSG_ANIMATE);
        long now = SystemClock.uptimeMillis();
        mAnimationLastTime = now;
        mCurrentAnimationTime = now + ANIMATION_FRAME_DURATION;
        mAnimating = true;
    } else {
        if (mAnimating) {
            mAnimating = false;
            mHandler.removeMessages(MSG_ANIMATE);
        }
        moveHandle(position);
    }
}
 
Example 42
Project: boohee_v5.6   File: NestedScrollView.java   Source Code and License 5 votes vote down vote up
private void initOrResetVelocityTracker() {
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    } else {
        this.mVelocityTracker.clear();
    }
}
 
Example 43
Project: NeoTerm   File: AbstractDragHandler.java   Source Code and License 5 votes vote down vote up
/**
 * Handles, when a drag gesture has been started.
 *
 * @param event
 *         The motion event, which started the drag gesture, as an instance of the class {@link
 *         MotionEvent}. The motion event may not be null
 */
private void handleDown(@NonNull final MotionEvent event) {
    pointerId = event.getPointerId(0);

    if (velocityTracker == null) {
        velocityTracker = VelocityTracker.obtain();
    } else {
        velocityTracker.clear();
    }

    velocityTracker.addMovement(event);
}
 
Example 44
Project: Instincts-2k17   File: WheelView.java   Source Code and License 5 votes vote down vote up
private void startWheelDrag(MotionEvent event, float x, float y) {
    mIsDraggingWheel = true;
    mDraggedAngle = 0f;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    mVelocityTracker.addMovement(event);

    mAngularVelocity = 0f;
    mLastTouchAngle = mWheelBounds.angleToDegrees(x, y);
}
 
Example 45
Project: YCCardView   File: CardViewLayout.java   Source Code and License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            // 此处说明底层没有子View愿意消费Touch事件
            break;

        case MotionEvent.ACTION_MOVE:
            int currentX = (int) event.getX();
            int dx = (int) (currentX - lastX);
            requireScrollChange(dx);
            lastX = currentX;
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocity = (int) velocityTracker.getXVelocity();
            recycleVelocityTracker();

            onRelease(event.getX(), velocity);
            break;
    }
    return true;
}
 
Example 46
Project: NeoTerm   File: AbstractDragHandler.java   Source Code and License 5 votes vote down vote up
/**
 * Handles a touch event.
 *
 * @param event
 *         The event, which should be handled, as an instance of the class {@link MotionEvent}.
 *         The event may be not null
 * @return True, if the event has been handled, false otherwise
 */
public final boolean handleTouchEvent(@NonNull final MotionEvent event) {
    ensureNotNull(event, "The motion event may not be null");

    if (tabSwitcher.isSwitcherShown() && !tabSwitcher.isEmpty()) {
        notifyOnCancelFling();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handleDown(event);
                return true;
            case MotionEvent.ACTION_MOVE:
                if (!tabSwitcher.isAnimationRunning() && event.getPointerId(0) == pointerId) {
                    if (velocityTracker == null) {
                        velocityTracker = VelocityTracker.obtain();
                    }

                    velocityTracker.addMovement(event);
                    handleDrag(arithmetics.getPosition(Axis.DRAGGING_AXIS, event),
                            arithmetics.getPosition(Axis.ORTHOGONAL_AXIS, event));
                } else {
                    handleRelease(null, dragThreshold);
                    handleDown(event);
                }

                return true;
            case MotionEvent.ACTION_UP:
                if (!tabSwitcher.isAnimationRunning() && event.getPointerId(0) == pointerId) {
                    handleRelease(event, dragThreshold);
                }

                return true;
            default:
                break;
        }
    }

    return false;
}
 
Example 47
Project: SwipeFinishLayout   File: SwipeFinishLayout.java   Source Code and License 5 votes vote down vote up
/**
 * 开始追踪速度
 * @param event
 */
private void startVelocityTracker(MotionEvent event) {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
}
 
Example 48
Project: GitHub   File: FreeFlowContainer.java   Source Code and License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

	super.onTouchEvent(event);
	if (mLayout == null) {
		return false;
	}

	// flag to check if laid out items are wide or tall enough
	// to require scrolling
	boolean canScroll = false;

	if (mLayout.horizontalScrollEnabled()
			&& this.mLayout.getContentWidth() > getWidth()) {
		canScroll = true;
	}
	if (mLayout.verticalScrollEnabled()
			&& mLayout.getContentHeight() > getHeight()) {
		canScroll = true;
	}

	switch (event.getAction()) {
	case (MotionEvent.ACTION_DOWN):
		touchDown(event);
		break;
	case (MotionEvent.ACTION_MOVE):
		if (canScroll) {
			touchMove(event);
		}
		break;
	case (MotionEvent.ACTION_UP):
		touchUp(event);
		break;
	case (MotionEvent.ACTION_CANCEL):
		touchCancel(event);
		break;
	}

	if (!canScroll) {
		return true;
	}

	if (mVelocityTracker == null && canScroll) {
		mVelocityTracker = VelocityTracker.obtain();
	}
	if (mVelocityTracker != null) {
		mVelocityTracker.addMovement(event);
	}

	return true;

}
 
Example 49
Project: GitHub   File: VersionedGestureDetector.java   Source Code and License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
	switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			mVelocityTracker = VelocityTracker.obtain();
			mVelocityTracker.addMovement(ev);

			mLastTouchX = getActiveX(ev);
			mLastTouchY = getActiveY(ev);
			mIsDragging = false;
			break;
		}

		case MotionEvent.ACTION_MOVE: {
			final float x = getActiveX(ev);
			final float y = getActiveY(ev);
			final float dx = x - mLastTouchX, dy = y - mLastTouchY;

			if (!mIsDragging) {
				// Use Pythagoras to see if drag length is larger than
				// touch slop
				mIsDragging = (float)Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;

			}

			if (mIsDragging) {
				mListener.onDrag(dx, dy);
				mLastTouchX = x;
				mLastTouchY = y;

				if (null != mVelocityTracker) {
					mVelocityTracker.addMovement(ev);
				}
			}
			break;
		}

		case MotionEvent.ACTION_CANCEL: {
			// Recycle Velocity Tracker
			if (null != mVelocityTracker) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			break;
		}

		case MotionEvent.ACTION_UP: {
			if (mIsDragging) {
				if (null != mVelocityTracker) {
					mLastTouchX = getActiveX(ev);
					mLastTouchY = getActiveY(ev);

					// Compute velocity within the last 1000ms
					mVelocityTracker.addMovement(ev);
					mVelocityTracker.computeCurrentVelocity(1000);

					final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity();

					// If the velocity is greater than minVelocity, call
					// listener
					if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
						mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY);
					}
				}
			}

			// Recycle Velocity Tracker
			if (null != mVelocityTracker) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			break;
		}
	}

	return true;
}
 
Example 50
Project: GitHub   File: OverScrollProcessor.java   Source Code and License 4 votes vote down vote up
private void obtainTracker(MotionEvent event) {
    if (null == moveTracker) {
        moveTracker = VelocityTracker.obtain();
    }
    moveTracker.addMovement(event);
}
 
Example 51
Project: GitHub   File: BGAStickyNavLayout.java   Source Code and License 4 votes vote down vote up
private void initVelocityTrackerIfNotExists() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
}
 
Example 52
Project: BooheeRuler   File: HorizontalRuler.java   Source Code and License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    //开始速度检测
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mOverScroller.isFinished()) {
                mOverScroller.abortAnimation();
            }

            mLastX = currentX;
            break;
        case MotionEvent.ACTION_MOVE:
            float moveX = mLastX - currentX;
            mLastX = currentX;
            scrollBy((int) (moveX), 0);
            break;
        case MotionEvent.ACTION_UP:
            //处理松手后的Fling
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) mVelocityTracker.getXVelocity();
            if (Math.abs(velocityX) > mMinimumVelocity) {
                fling(-velocityX);
            } else {
                scrollBackToCurrentScale();
            }
            //VelocityTracker回收
            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            releaseEdgeEffects();
            break;
        case MotionEvent.ACTION_CANCEL:
            if (!mOverScroller.isFinished()) {
                mOverScroller.abortAnimation();
            }
            //回滚到整点刻度
            scrollBackToCurrentScale();
            //VelocityTracker回收
            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            releaseEdgeEffects();
            break;
    }
    return true;
}
 
Example 53
Project: GitHub   File: SlideLayout.java   Source Code and License 4 votes vote down vote up
private void createVelocityTracker(MotionEvent event) {
	if (mVelocityTracker == null) {
		mVelocityTracker = VelocityTracker.obtain();
	}
	mVelocityTracker.addMovement(event);
}
 
Example 54
Project: pius1   File: SlideBottomPanel.java   Source Code and License 4 votes vote down vote up
private void initVelocityTracker(MotionEvent event) {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
}
 
Example 55
Project: zabbkit-android   File: DraggedPanelLayout.java   Source Code and License 4 votes vote down vote up
private void obtainVelocityTracker() {
	if (velocityTracker == null) {
		velocityTracker = VelocityTracker.obtain();
	}
}
 
Example 56
Project: AndroidPDF   File: DocumentView.java   Source Code and License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);

    if (multiTouchZoom != null) {
        if (multiTouchZoom.onTouchEvent(ev)) {
            return true;
        }

        if (multiTouchZoom.isResetLastPointAfterZoom()) {
            setLastPosition(ev);
            multiTouchZoom.setResetLastPointAfterZoom(false);
        }
    }

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

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            stopScroller();
            setLastPosition(ev);
            if (ev.getEventTime() - lastDownEventTime < DOUBLE_TAP_TIME) {
                zoomModel.toggleZoomControls();
            } else {
                lastDownEventTime = ev.getEventTime();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            scrollBy((int) (lastX - ev.getX()), (int) (lastY - ev.getY()));
            setLastPosition(ev);
            break;
        case MotionEvent.ACTION_UP:
            velocityTracker.computeCurrentVelocity(1000);
            scroller.fling(getScrollX(), getScrollY(), (int) -velocityTracker.getXVelocity(), (int) -velocityTracker.getYVelocity(), getLeftLimit(), getRightLimit(), getTopLimit(), getBottomLimit());
            velocityTracker.recycle();
            velocityTracker = null;

            break;
    }
    return true;
}
 
Example 57
Project: LaunchEnr   File: SwipeHelper.java   Source Code and License 4 votes vote down vote up
private float getPerpendicularVelocity(VelocityTracker vt) {
    return mSwipeDirection == X ? vt.getYVelocity() :
            vt.getXVelocity();
}
 
Example 58
Project: Android-Music-Player   File: libraryHome.java   Source Code and License 4 votes vote down vote up
public libraryHome(Context context, int width, int height) {
    super(context, width, height);
    setEnableCatch();
    This = this;
    //setBackgroundColor(com.shape.home.backgroundImg.Color0);


    //TopStick = new FMView(context,width,Ui.cd.getHt(2));
    //TopStick.setBackgroundColor(backgroundImg.Color0);
    //addView(TopStick);

   /* TopView = new topView(context,Ui.cd.getHt(25),Ui.cd.getHt(50));
    TopView.setBackgroundColor(0x00FFFFFF);
    TopView.setX((width - TopView.width) / 2);
    addView(TopView);*/

    BtmView = new btmView(context,width, Ui.cd.getHt(60)){
        @Override
        int openMenu(float scolled) {
            int no = super.openMenu(scolled);
            libraryHome.this.openMenu(no);
            return no;
        }
    };
    BtmView.setBackgroundColor(backgroundImg.Color0);
    BtmView.setY(height - BtmView.height);
    addView(BtmView);

    //drawCatch();
    LibreryPage = new Librery(getContext(),width,height - BtmView.height,0);
    SongsPage = new Songs(getContext(),width,height - BtmView.height,1);
    AlbumsPage = new Albums(getContext(),width,height - BtmView.height,2);
    ArtistPage = new Artist(getContext(),width,height - BtmView.height,3);
    GenersPage = new Geners(getContext(),width,height - BtmView.height,4);
    FolderPage = new Folder(getContext(),width,height - BtmView.height,5);

    Sp = SongsPage;
    addView(Sp,indexOfChild(BtmView));


    //openMenu(0);
    SongSlider = new songSlider(context,width, Ui.cd.getHt(22));
    SongSlider.setY(height - BtmView.height - (SongSlider.height / 2f));
    SongSlider.setBtns(true);
    addView(SongSlider);

    Vx = VelocityTracker.obtain();

    pages = new pagePrototype[]{LibreryPage,SongsPage,AlbumsPage,ArtistPage,GenersPage,FolderPage};

    //addView(top,indexOfChild(Sp)+1);
    //addView(LibreryPage);

}
 
Example 59
Project: GCSApp   File: VersionedGestureDetector.java   Source Code and License 4 votes vote down vote up
@Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    mVelocityTracker = VelocityTracker.obtain();
                    mVelocityTracker.addMovement(ev);

                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);
                    mIsDragging = false;
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    final float x = getActiveX(ev);
                    final float y = getActiveY(ev);
                    final float dx = x - mLastTouchX, dy = y - mLastTouchY;

                    if (!mIsDragging) {
                        // Use Pythagoras to see if drag length is larger than
                        // touch slop
//						mIsDragging = FloatMath.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
                        mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
                    }

                    if (mIsDragging) {
                        mListener.onDrag(dx, dy);
                        mLastTouchX = x;
                        mLastTouchY = y;

                        if (null != mVelocityTracker) {
                            mVelocityTracker.addMovement(ev);
                        }
                    }
                    break;
                }

                case MotionEvent.ACTION_CANCEL: {
                    // Recycle Velocity Tracker
                    if (null != mVelocityTracker) {
                        mVelocityTracker.recycle();
                        mVelocityTracker = null;
                    }
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    if (mIsDragging) {
                        if (null != mVelocityTracker) {
                            mLastTouchX = getActiveX(ev);
                            mLastTouchY = getActiveY(ev);

                            // Compute velocity within the last 1000ms
                            mVelocityTracker.addMovement(ev);
                            mVelocityTracker.computeCurrentVelocity(1000);

                            final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity();

                            // If the velocity is greater than minVelocity, call
                            // listener
                            if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                                mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY);
                            }
                        }
                    }

                    // Recycle Velocity Tracker
                    if (null != mVelocityTracker) {
                        mVelocityTracker.recycle();
                        mVelocityTracker = null;
                    }
                    break;
                }
            }

            return true;
        }
 
Example 60
Project: baselibrary-master   File: NumberPickerView.java   Source Code and License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if(mItemHeight == 0) return true;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    currY = event.getY();

    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            mFlagMayPress = true;
            mHandlerInNewThread.removeMessages(HANDLER_WHAT_REFRESH);
            stopScrolling();
            downY = currY;
            downYGlobal = mCurrDrawGlobalY;
            onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            getParent().requestDisallowInterceptTouchEvent(true);
            break;
        case MotionEvent.ACTION_MOVE:
            float spanY = downY - currY;

            if(mFlagMayPress && (-mScaledTouchSlop < spanY && spanY < mScaledTouchSlop)){

            }else{
                mFlagMayPress = false;
                mCurrDrawGlobalY = limitY((int)(downYGlobal + spanY));
                calculateFirstItemParameterByGlobalY();
                invalidate();
            }
            onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            break;
        case MotionEvent.ACTION_UP:
            if(mFlagMayPress){
                click(event);
            }else {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000);
                int velocityY = (int) (velocityTracker.getYVelocity() * mFriction);
                if (Math.abs(velocityY) > mMiniVelocityFling) {
                    mScroller.fling(0, mCurrDrawGlobalY, 0, -velocityY,
                            Integer.MIN_VALUE, Integer.MAX_VALUE, limitY(Integer.MIN_VALUE), limitY(Integer.MAX_VALUE));
                    invalidate();
                    onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
                }
                mHandlerInNewThread.sendMessageDelayed(getMsg(HANDLER_WHAT_REFRESH), 0);
                releaseVelocityTracker();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            downYGlobal = mCurrDrawGlobalY;
            stopScrolling();
            mHandlerInNewThread.sendMessageDelayed(getMsg(HANDLER_WHAT_REFRESH), 0);
            break;
    }
    return true ;
}