Code example for Scroller

Methods: abortAnimation, fling, isFinished

0
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        final int x = (int) ev.getX();
        if (action == MotionEvent.ACTION_DOWN) {
            mDragMode = true;
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            } 
        } 
 
        if (!mDragMode)
            return super.onTouchEvent(ev);
 
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } 
        mVelocityTracker.addMovement(ev);
 
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break; 
            case MotionEvent.ACTION_MOVE:
                mScrollX += mLastX - x;
                reScrollTo(mScrollX, 0, true);
                break; 
            case MotionEvent.ACTION_UP:
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000);
                int initialVelocity = (int) Math.min(velocityTracker.getXVelocity(), mMaximumVelocity);
 
                if (getChildCount() > 0 && Math.abs(initialVelocity) > mMinimumVelocity) {
                    fling(-initialVelocity);
                } 
            case MotionEvent.ACTION_CANCEL:
            default: 
                mDragMode = false;
 
        } 
        mLastX = x;
 
        return true; 
    } 
 
    /** 
     * causes the underlying mScroller to do a fling action which will be recovered in the 
     * computeScroll method 
     * 
     * @param velocityX the speed of the fling 
     */ 
    private void fling(int velocityX) {
        if (getChildCount() > 0) {
            mScroller.fling(mScrollX, 0, velocityX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
            invalidate();
        } 
    } 
 
    public void setOnScrollListener(OnScrollListener l) {