Class OverScroller

java.lang.Object
icyllis.modernui.widget.OverScroller

public class OverScroller extends Object
This class controls horizontal and vertical scrolling with the ability to overshoot the bounds of a scrolling operation.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a Scroller with flywheel.
     
    OverScroller(TimeInterpolator interpolator, boolean flywheel)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Stops the animation.
    boolean
    Call this when you want to know the new location.
    void
    fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)
     
    void
    fling(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.
    final void
    forceFinished(boolean finished)
    Force the finished field to a particular value.
    float
    Returns the absolute value of the current velocity.
    final int
    Returns the current X offset in the scroll.
    final int
    Returns the current Y offset in the scroll.
    final int
    Returns how long the scroll event will take, in milliseconds.
    final int
    Returns where the scroll will end.
    final int
    Returns where the scroll will end.
    final int
    Returns the start X offset in the scroll.
    final int
    Returns the start Y offset in the scroll.
    final boolean
    Returns whether the scroller has finished scrolling.
    boolean
    Returns whether the current Scroller is currently returning to a valid position.
    boolean
    isScrollingInDirection(float xvel, float yvel)
     
    void
    notifyHorizontalEdgeReached(int startX, int finalX, int overX)
    Notify the scroller that we've reached a horizontal boundary.
    void
    notifyVerticalEdgeReached(int startY, int finalY, int overY)
    Notify the scroller that we've reached a vertical boundary.
    void
    setFinalX(int newX)
    Sets the final position (X) for this scroller.
    void
    setFinalY(int newY)
    Sets the final position (Y) for this scroller.
    final void
    setFriction(float friction)
    The amount of friction applied to flings.
    boolean
    springBack(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.
    void
    startScroll(int startX, int startY, int dx, int dy)
    Start scrolling by providing a starting point and the distance to travel.
    void
    startScroll(int startX, int startY, int dx, int dy, int duration)
    Start scrolling by providing a starting point and the distance to travel.
    int
    Returns the time elapsed since the beginning of the scrolling.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • OverScroller

      public OverScroller()
      Creates a Scroller with flywheel.
    • OverScroller

      public OverScroller(@Nullable TimeInterpolator interpolator)
    • OverScroller

      public OverScroller(@Nullable TimeInterpolator interpolator, boolean flywheel)
  • Method Details

    • setFriction

      public final void setFriction(float friction)
      The amount of friction applied to flings.
      Parameters:
      friction - A scalar dimension-less value representing the coefficient of friction.
    • isFinished

      public final boolean isFinished()
      Returns whether the scroller has finished scrolling.
      Returns:
      True if the scroller has finished scrolling, false otherwise.
    • forceFinished

      public final void forceFinished(boolean finished)
      Force the finished field to a particular value. Contrary to abortAnimation(), forcing the animation to finished does NOT cause the scroller to move to the final x and y position.
      Parameters:
      finished - The new finished value.
    • getDuration

      public final int getDuration()
      Returns how long the scroll event will take, in milliseconds.
      Returns:
      The duration of the scroll in milliseconds.
    • setFinalX

      @Internal public void setFinalX(int newX)
      Sets the final position (X) for this scroller.
      Parameters:
      newX - The new X offset as an absolute distance from the origin.
      See Also:
    • setFinalY

      @Internal public void setFinalY(int newY)
      Sets the final position (Y) for this scroller.
      Parameters:
      newY - The new Y offset as an absolute distance from the origin.
      See Also:
    • getCurrX

      public final int getCurrX()
      Returns the current X offset in the scroll.
      Returns:
      The new X offset as an absolute distance from the origin.
    • getCurrY

      public final int getCurrY()
      Returns the current Y offset in the scroll.
      Returns:
      The new Y offset as an absolute distance from the origin.
    • getCurrVelocity

      public float getCurrVelocity()
      Returns the absolute value of the current velocity.
      Returns:
      The original velocity less the deceleration, norm of the X and Y velocity vector.
    • getStartX

      public final int getStartX()
      Returns the start X offset in the scroll.
      Returns:
      The start X offset as an absolute distance from the origin.
    • getStartY

      public final int getStartY()
      Returns the start Y offset in the scroll.
      Returns:
      The start Y offset as an absolute distance from the origin.
    • getFinalX

      public final int getFinalX()
      Returns where the scroll will end. Valid only for "fling" scrolls.
      Returns:
      The final X offset as an absolute distance from the origin.
    • getFinalY

      public final int getFinalY()
      Returns where the scroll will end. Valid only for "fling" scrolls.
      Returns:
      The final Y offset as an absolute distance from the origin.
    • computeScrollOffset

      public boolean computeScrollOffset()
      Call this when you want to know the new location. If it returns true, the animation is not yet finished.
    • startScroll

      public void startScroll(int startX, int startY, int dx, int dy)
      Start scrolling by providing a starting point and the distance to travel.
      Parameters:
      startX - Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
      startY - Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
      dx - Horizontal distance to travel. Positive numbers will scroll the content to the left.
      dy - Vertical distance to travel. Positive numbers will scroll the content up.
    • startScroll

      public void startScroll(int startX, int startY, int dx, int dy, int duration)
      Start scrolling by providing a starting point and the distance to travel.
      Parameters:
      startX - Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
      startY - Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
      dx - Horizontal distance to travel. Positive numbers will scroll the content to the left.
      dy - Vertical distance to travel. Positive numbers will scroll the content up.
      duration - Duration of the scroll in milliseconds.
    • springBack

      public boolean springBack(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.
      Parameters:
      startX - Starting X coordinate
      startY - Starting Y coordinate
      minX - Minimum valid X value
      maxX - Maximum valid X value
      minY - Minimum valid Y value
      maxY - Minimum valid Y value
      Returns:
      true if a springback was initiated, false if startX and startY were already within the valid range.
    • fling

      public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)
    • fling

      public void fling(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.
      Parameters:
      startX - Starting point of the scroll (X)
      startY - Starting point of the scroll (Y)
      velocityX - Initial velocity of the fling (X) measured in pixels per second.
      velocityY - Initial velocity of the fling (Y) measured in pixels per second
      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.
      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.
      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.
      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.
      overX - Overfling range. If > 0, horizontal overfling in either direction will be possible.
      overY - Overfling range. If > 0, vertical overfling in either direction will be possible.
    • notifyHorizontalEdgeReached

      public void notifyHorizontalEdgeReached(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.
      Parameters:
      startX - Starting/current X position
      finalX - Desired final X position
      overX - Magnitude of overscroll allowed. This should be the maximum desired distance from finalX. Absolute value - must be positive.
    • notifyVerticalEdgeReached

      public void notifyVerticalEdgeReached(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.
      Parameters:
      startY - Starting/current Y position
      finalY - Desired final Y position
      overY - Magnitude of overscroll allowed. This should be the maximum desired distance from finalY. Absolute value - must be positive.
    • isOverScrolled

      public boolean isOverScrolled()
      Returns whether the current Scroller is currently returning to a valid position. Valid bounds were provided by the fling(int, int, int, int, int, int, int, int, int, int) method.

      One should check this value before calling 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.

      Returns:
      true when the current position is overscrolled and in the process of interpolating back to a valid value.
    • abortAnimation

      public void abortAnimation()
      Stops the animation. Contrary to forceFinished(boolean), aborting the animating causes the scroller to move to the final x and y positions.
      See Also:
    • timePassed

      public int timePassed()
      Returns the time elapsed since the beginning of the scrolling.
      Returns:
      The elapsed time in milliseconds.
    • isScrollingInDirection

      public boolean isScrollingInDirection(float xvel, float yvel)