FileDocCategorySizeDatePackage
OverScroller.javaAPI DocAndroid 5.1 API37238Thu Mar 12 22:22:10 GMT 2015android.widget

OverScroller

public class OverScroller extends Object
This class encapsulates scrolling with the ability to overshoot the bounds of a scrolling operation. This class is a drop-in replacement for {@link android.widget.Scroller} in most cases.

Fields Summary
private int
mMode
private final SplineOverScroller
mScrollerX
private final SplineOverScroller
mScrollerY
private android.view.animation.Interpolator
mInterpolator
private final boolean
mFlywheel
private static final int
DEFAULT_DURATION
private static final int
SCROLL_MODE
private static final int
FLING_MODE
Constructors Summary
public OverScroller(android.content.Context context)
Creates an OverScroller with a viscous fluid scroll interpolator and flywheel.

param
context


                      
       
        this(context, null);
    
public OverScroller(android.content.Context context, android.view.animation.Interpolator interpolator)
Creates an OverScroller with flywheel enabled.

param
context The context of this application.
param
interpolator The scroll interpolator. If null, a default (viscous) interpolator will be used.

        this(context, interpolator, true);
    
public OverScroller(android.content.Context context, android.view.animation.Interpolator interpolator, boolean flywheel)
Creates an OverScroller.

param
context The context of this application.
param
interpolator The scroll interpolator. If null, a default (viscous) interpolator will be used.
param
flywheel If true, successive fling motions will keep on increasing scroll speed.
hide

        if (interpolator == null) {
            mInterpolator = new Scroller.ViscousFluidInterpolator();
        } else {
            mInterpolator = interpolator;
        }
        mFlywheel = flywheel;
        mScrollerX = new SplineOverScroller(context);
        mScrollerY = new SplineOverScroller(context);
    
public OverScroller(android.content.Context context, android.view.animation.Interpolator interpolator, float bounceCoefficientX, float bounceCoefficientY)
Creates an OverScroller with flywheel enabled.

param
context The context of this application.
param
interpolator The scroll interpolator. If null, a default (viscous) interpolator will be used.
param
bounceCoefficientX A value between 0 and 1 that will determine the proportion of the velocity which is preserved in the bounce when the horizontal edge is reached. A null value means no bounce. This behavior is no longer supported and this coefficient has no effect.
param
bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This behavior is no longer supported and this coefficient has no effect. !deprecated Use {!link #OverScroller(Context, Interpolator, boolean)} instead.

        this(context, interpolator, true);
    
public OverScroller(android.content.Context context, android.view.animation.Interpolator interpolator, float bounceCoefficientX, float bounceCoefficientY, boolean flywheel)
Creates an OverScroller.

param
context The context of this application.
param
interpolator The scroll interpolator. If null, a default (viscous) interpolator will be used.
param
bounceCoefficientX A value between 0 and 1 that will determine the proportion of the velocity which is preserved in the bounce when the horizontal edge is reached. A null value means no bounce. This behavior is no longer supported and this coefficient has no effect.
param
bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This behavior is no longer supported and this coefficient has no effect.
param
flywheel If true, successive fling motions will keep on increasing scroll speed. !deprecated Use {!link OverScroller(Context, Interpolator, boolean)} instead.

        this(context, interpolator, flywheel);
    
Methods Summary
public voidabortAnimation()
Stops the animation. Contrary to {@link #forceFinished(boolean)}, aborting the animating causes the scroller to move to the final x and y positions.

see
#forceFinished(boolean)

        mScrollerX.finish();
        mScrollerY.finish();
    
public booleancomputeScrollOffset()
Call this when you want to know the new location. If it returns true, the animation is not yet finished.

        if (isFinished()) {
            return false;
        }

        switch (mMode) {
            case SCROLL_MODE:
                long time = AnimationUtils.currentAnimationTimeMillis();
                // Any scroller can be used for time, since they were started
                // together in scroll mode. We use X here.
                final long elapsedTime = time - mScrollerX.mStartTime;

                final int duration = mScrollerX.mDuration;
                if (elapsedTime < duration) {
                    final float q = mInterpolator.getInterpolation(elapsedTime / (float) duration);
                    mScrollerX.updateScroll(q);
                    mScrollerY.updateScroll(q);
                } else {
                    abortAnimation();
                }
                break;

            case FLING_MODE:
                if (!mScrollerX.mFinished) {
                    if (!mScrollerX.update()) {
                        if (!mScrollerX.continueWhenFinished()) {
                            mScrollerX.finish();
                        }
                    }
                }

                if (!mScrollerY.mFinished) {
                    if (!mScrollerY.update()) {
                        if (!mScrollerY.continueWhenFinished()) {
                            mScrollerY.finish();
                        }
                    }
                }

                break;
        }

        return true;
    
public voidextendDuration(int extend)
Extend the scroll animation. This allows a running animation to scroll further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}.

param
extend Additional time to scroll in milliseconds.
see
#setFinalX(int)
see
#setFinalY(int)
hide
Pending removal once nothing depends on it
deprecated
OverScrollers don't necessarily have a fixed duration. Instead of setting a new final position and extending the duration of an existing scroll, use startScroll to begin a new animation.

        mScrollerX.extendDuration(extend);
        mScrollerY.extendDuration(extend);
    
public voidfling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)

        fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0);
    
public voidfling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY, int overX, int overY)
Start scrolling based on a fling gesture. The distance traveled will depend on the initial velocity of the fling.

param
startX Starting point of the scroll (X)
param
startY Starting point of the scroll (Y)
param
velocityX Initial velocity of the fling (X) measured in pixels per second.
param
velocityY Initial velocity of the fling (Y) measured in pixels per second
param
minX Minimum X value. The scroller will not scroll past this point unless overX > 0. If overfling is allowed, it will use minX as a springback boundary.
param
maxX Maximum X value. The scroller will not scroll past this point unless overX > 0. If overfling is allowed, it will use maxX as a springback boundary.
param
minY Minimum Y value. The scroller will not scroll past this point unless overY > 0. If overfling is allowed, it will use minY as a springback boundary.
param
maxY Maximum Y value. The scroller will not scroll past this point unless overY > 0. If overfling is allowed, it will use maxY as a springback boundary.
param
overX Overfling range. If > 0, horizontal overfling in either direction will be possible.
param
overY Overfling range. If > 0, vertical overfling in either direction will be possible.

        // Continue a scroll or fling in progress
        if (mFlywheel && !isFinished()) {
            float oldVelocityX = mScrollerX.mCurrVelocity;
            float oldVelocityY = mScrollerY.mCurrVelocity;
            if (Math.signum(velocityX) == Math.signum(oldVelocityX) &&
                    Math.signum(velocityY) == Math.signum(oldVelocityY)) {
                velocityX += oldVelocityX;
                velocityY += oldVelocityY;
            }
        }

        mMode = FLING_MODE;
        mScrollerX.fling(startX, velocityX, minX, maxX, overX);
        mScrollerY.fling(startY, velocityY, minY, maxY, overY);
    
public final voidforceFinished(boolean finished)
Force the finished field to a particular value. Contrary to {@link #abortAnimation()}, forcing the animation to finished does NOT cause the scroller to move to the final x and y position.

param
finished The new finished value.

        mScrollerX.mFinished = mScrollerY.mFinished = finished;
    
public floatgetCurrVelocity()
Returns the absolute value of the current velocity.

return
The original velocity less the deceleration, norm of the X and Y velocity vector.

        float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity;
        squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity;
        return FloatMath.sqrt(squaredNorm);
    
public final intgetCurrX()
Returns the current X offset in the scroll.

return
The new X offset as an absolute distance from the origin.

        return mScrollerX.mCurrentPosition;
    
public final intgetCurrY()
Returns the current Y offset in the scroll.

return
The new Y offset as an absolute distance from the origin.

        return mScrollerY.mCurrentPosition;
    
public final intgetDuration()
Returns how long the scroll event will take, in milliseconds.

return
The duration of the scroll in milliseconds.
hide
Pending removal once nothing depends on it
deprecated
OverScrollers don't necessarily have a fixed duration. This function will lie to the best of its ability.

        return Math.max(mScrollerX.mDuration, mScrollerY.mDuration);
    
public final intgetFinalX()
Returns where the scroll will end. Valid only for "fling" scrolls.

return
The final X offset as an absolute distance from the origin.

        return mScrollerX.mFinal;
    
public final intgetFinalY()
Returns where the scroll will end. Valid only for "fling" scrolls.

return
The final Y offset as an absolute distance from the origin.

        return mScrollerY.mFinal;
    
public final intgetStartX()
Returns the start X offset in the scroll.

return
The start X offset as an absolute distance from the origin.

        return mScrollerX.mStart;
    
public final intgetStartY()
Returns the start Y offset in the scroll.

return
The start Y offset as an absolute distance from the origin.

        return mScrollerY.mStart;
    
public final booleanisFinished()
Returns whether the scroller has finished scrolling.

return
True if the scroller has finished scrolling, false otherwise.

        return mScrollerX.mFinished && mScrollerY.mFinished;
    
public booleanisOverScrolled()
Returns whether the current Scroller is currently returning to a valid position. Valid bounds were provided by the {@link #fling(int, int, int, int, int, int, int, int, int, int)} method. One should check this value before calling {@link #startScroll(int, int, int, int)} as the interpolation currently in progress to restore a valid position will then be stopped. The caller has to take into account the fact that the started scroll will start from an overscrolled position.

return
true when the current position is overscrolled and in the process of interpolating back to a valid value.

        return ((!mScrollerX.mFinished &&
                mScrollerX.mState != SplineOverScroller.SPLINE) ||
                (!mScrollerY.mFinished &&
                        mScrollerY.mState != SplineOverScroller.SPLINE));
    
public booleanisScrollingInDirection(float xvel, float yvel)

hide

        final int dx = mScrollerX.mFinal - mScrollerX.mStart;
        final int dy = mScrollerY.mFinal - mScrollerY.mStart;
        return !isFinished() && Math.signum(xvel) == Math.signum(dx) &&
                Math.signum(yvel) == Math.signum(dy);
    
public voidnotifyHorizontalEdgeReached(int startX, int finalX, int overX)
Notify the scroller that we've reached a horizontal boundary. Normally the information to handle this will already be known when the animation is started, such as in a call to one of the fling functions. However there are cases where this cannot be known in advance. This function will transition the current motion and animate from startX to finalX as appropriate.

param
startX Starting/current X position
param
finalX Desired final X position
param
overX Magnitude of overscroll allowed. This should be the maximum desired distance from finalX. Absolute value - must be positive.

        mScrollerX.notifyEdgeReached(startX, finalX, overX);
    
public voidnotifyVerticalEdgeReached(int startY, int finalY, int overY)
Notify the scroller that we've reached a vertical boundary. Normally the information to handle this will already be known when the animation is started, such as in a call to one of the fling functions. However there are cases where this cannot be known in advance. This function will animate a parabolic motion from startY to finalY.

param
startY Starting/current Y position
param
finalY Desired final Y position
param
overY Magnitude of overscroll allowed. This should be the maximum desired distance from finalY. Absolute value - must be positive.

        mScrollerY.notifyEdgeReached(startY, finalY, overY);
    
public voidsetFinalX(int newX)
Sets the final position (X) for this scroller.

param
newX The new X offset as an absolute distance from the origin.
see
#extendDuration(int)
see
#setFinalY(int)
hide
Pending removal once nothing depends on it
deprecated
OverScroller's final position may change during an animation. Instead of setting a new final position and extending the duration of an existing scroll, use startScroll to begin a new animation.

        mScrollerX.setFinalPosition(newX);
    
public voidsetFinalY(int newY)
Sets the final position (Y) for this scroller.

param
newY The new Y offset as an absolute distance from the origin.
see
#extendDuration(int)
see
#setFinalX(int)
hide
Pending removal once nothing depends on it
deprecated
OverScroller's final position may change during an animation. Instead of setting a new final position and extending the duration of an existing scroll, use startScroll to begin a new animation.

        mScrollerY.setFinalPosition(newY);
    
public final voidsetFriction(float friction)
The amount of friction applied to flings. The default value is {@link ViewConfiguration#getScrollFriction}.

param
friction A scalar dimension-less value representing the coefficient of friction.

        mScrollerX.setFriction(friction);
        mScrollerY.setFriction(friction);
    
voidsetInterpolator(android.view.animation.Interpolator interpolator)

        if (interpolator == null) {
            mInterpolator = new Scroller.ViscousFluidInterpolator();
        } else {
            mInterpolator = interpolator;
        }
    
public booleanspringBack(int startX, int startY, int minX, int maxX, int minY, int maxY)
Call this when you want to 'spring back' into a valid coordinate range.

param
startX Starting X coordinate
param
startY Starting Y coordinate
param
minX Minimum valid X value
param
maxX Maximum valid X value
param
minY Minimum valid Y value
param
maxY Minimum valid Y value
return
true if a springback was initiated, false if startX and startY were already within the valid range.

        mMode = FLING_MODE;

        // Make sure both methods are called.
        final boolean spingbackX = mScrollerX.springback(startX, minX, maxX);
        final boolean spingbackY = mScrollerY.springback(startY, minY, maxY);
        return spingbackX || spingbackY;
    
public voidstartScroll(int startX, int startY, int dx, int dy)
Start scrolling by providing a starting point and the distance to travel. The scroll will use the default value of 250 milliseconds for the duration.

param
startX Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
param
startY Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
param
dx Horizontal distance to travel. Positive numbers will scroll the content to the left.
param
dy Vertical distance to travel. Positive numbers will scroll the content up.

        startScroll(startX, startY, dx, dy, DEFAULT_DURATION);
    
public voidstartScroll(int startX, int startY, int dx, int dy, int duration)
Start scrolling by providing a starting point and the distance to travel.

param
startX Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
param
startY Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
param
dx Horizontal distance to travel. Positive numbers will scroll the content to the left.
param
dy Vertical distance to travel. Positive numbers will scroll the content up.
param
duration Duration of the scroll in milliseconds.

        mMode = SCROLL_MODE;
        mScrollerX.startScroll(startX, dx, duration);
        mScrollerY.startScroll(startY, dy, duration);
    
public inttimePassed()
Returns the time elapsed since the beginning of the scrolling.

return
The elapsed time in milliseconds.
hide

        final long time = AnimationUtils.currentAnimationTimeMillis();
        final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime);
        return (int) (time - startTime);