Code example for Scroller

Methods: abortAnimation, getDuration, isFinished, startScroll

0
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        // check for tap and cancel fling 
        if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
            if (!scroller.isFinished()) scroller.abortAnimation();
        } 
 
        // handle pinch zoom gesture 
        // don't check return value since it is always true 
        scaleGestureDetector.onTouchEvent(event);
 
        // check for scroll gesture 
        if (gestureDetector.onTouchEvent(event)) return true;
 
        // check for pointer release 
        if ((event.getPointerCount() == 1) && ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)) {
            int newScrollX = getScrollX();
            if (getScaledWidth() < getWidth()) newScrollX = -(getWidth() - getScaledWidth()) / 2;
            else if (getScrollX() < 0) newScrollX = 0;
            else if (getScrollX() > getScaledWidth() - getWidth()) newScrollX = getScaledWidth() - getWidth();
 
            int newScrollY = getScrollY();
            if (getScaledHeight() < getHeight()) newScrollY = -(getHeight() - getScaledHeight()) / 2;
            else if (getScrollY() < 0) newScrollY = 0;
            else if (getScrollY() > getScaledHeight() - getHeight()) newScrollY = getScaledHeight() - getHeight();
 
            if ((newScrollX != getScrollX()) || (newScrollY != getScrollY())) {
                scroller.startScroll(getScrollX(), getScrollY(), newScrollX - getScrollX(), newScrollY - getScrollY());
                awakenScrollBars(scroller.getDuration());
            } 
        } 
 
        return true; 
    }