public class Scroller extends Object
This class encapsulates scrolling. You can use scrollers (Scroller
or OverScroller
) to collect the data you need to produce a scrolling
animation—for example, in response to a fling gesture. Scrollers track
scroll offsets for you over time, but they don't automatically apply those
positions to your view. It's your responsibility to get and apply new
coordinates at a rate that will make the scrolling animation look smooth.
Here is a simple example:
private Scroller mScroller = new Scroller(context); ... public void zoomIn() { // Revert any animation currently in progress mScroller.forceFinished(true); // Start scrolling by providing a starting point and // the distance to travel mScroller.startScroll(0, 0, 100, 0); // Invalidate to request a redraw invalidate(); }
To track the changing positions of the x/y coordinates, use
computeScrollOffset()
. The method returns a boolean to indicate
whether the scroller is finished. If it isn't, it means that a fling or
programmatic pan operation is still in progress. You can use this method to
find the current offsets of the x and y coordinates, for example:
if (mScroller.computeScrollOffset()) { // Get current x and y positions int currX = mScroller.getCurrX(); int currY = mScroller.getCurrY(); ... }
Constructor and Description |
---|
Scroller(Context context)
Create a Scroller with the default duration and interpolator.
|
Scroller(Context context,
Interpolator interpolator)
Create a Scroller with the specified interpolator.
|
Scroller(Context context,
Interpolator interpolator,
boolean flywheel)
Create a Scroller with the specified interpolator.
|
Modifier and Type | Method and Description |
---|---|
void |
abortAnimation()
Stops the animation.
|
boolean |
computeScrollOffset()
Call this when you want to know the new location.
|
void |
extendDuration(int extend)
Extend the scroll animation.
|
void |
fling(int startX,
int startY,
int velocityX,
int velocityY,
int minX,
int maxX,
int minY,
int maxY)
Start scrolling based on a fling gesture.
|
void |
forceFinished(boolean finished)
Force the finished field to a particular value.
|
float |
getCurrVelocity()
Returns the current velocity.
|
int |
getCurrX()
Returns the current X offset in the scroll.
|
int |
getCurrY()
Returns the current Y offset in the scroll.
|
int |
getDuration()
Returns how long the scroll event will take, in milliseconds.
|
int |
getFinalX()
Returns where the scroll will end.
|
int |
getFinalY()
Returns where the scroll will end.
|
int |
getStartX()
Returns the start X offset in the scroll.
|
int |
getStartY()
Returns the start Y offset in the scroll.
|
boolean |
isFinished()
Returns whether the scroller has finished scrolling.
|
boolean |
isScrollingInDirection(float xvel,
float yvel) |
void |
setFinalX(int newX)
Sets the final position (X) for this scroller.
|
void |
setFinalY(int newY)
Sets the final position (Y) for this scroller.
|
void |
setFriction(float friction)
The amount of friction applied to flings.
|
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, the distance to travel,
and the duration of the scroll.
|
int |
timePassed()
Returns the time elapsed since the beginning of the scrolling.
|
public Scroller(Context context)
public Scroller(Context context, Interpolator interpolator)
public Scroller(Context context, Interpolator interpolator, boolean flywheel)
public final void setFriction(float friction)
ViewConfiguration.getScrollFriction()
.friction
- A scalar dimension-less value representing the coefficient of
friction.public final boolean isFinished()
public final void forceFinished(boolean finished)
finished
- The new finished value.public final int getDuration()
public final int getCurrX()
public final int getCurrY()
public float getCurrVelocity()
public final int getStartX()
public final int getStartY()
public final int getFinalX()
public final int getFinalY()
public boolean computeScrollOffset()
public void startScroll(int startX, int startY, int dx, int dy)
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.public void startScroll(int startX, int startY, int dx, int dy, int duration)
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.public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)
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
secondminX
- Minimum X value. The scroller will not scroll past this
point.maxX
- Maximum X value. The scroller will not scroll past this
point.minY
- Minimum Y value. The scroller will not scroll past this
point.maxY
- Maximum Y value. The scroller will not scroll past this
point.public void abortAnimation()
forceFinished(boolean)
,
aborting the animating cause the scroller to move to the final x and y
positionforceFinished(boolean)
public void extendDuration(int extend)
setFinalX(int)
or setFinalY(int)
.extend
- Additional time to scroll in milliseconds.setFinalX(int)
,
setFinalY(int)
public int timePassed()
public void setFinalX(int newX)
newX
- The new X offset as an absolute distance from the origin.extendDuration(int)
,
setFinalY(int)
public void setFinalY(int newY)
newY
- The new Y offset as an absolute distance from the origin.extendDuration(int)
,
setFinalX(int)
public boolean isScrollingInDirection(float xvel, float yvel)