public class ScrollableScroller
extends java.lang.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 |
|---|
ScrollableScroller(android.content.Context context,
android.view.animation.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 |
fling(int startX,
int startY,
int velocityX,
int velocityY,
int minX,
int maxX,
int minY,
int maxY)
Start scrolling based on a fling gesture.
|
float |
getCurrVelocity()
Returns the current velocity.
|
int |
getCurrY()
Returns the current Y offset in the scroll.
|
int |
getDuration()
Returns how long the scroll event will take, in milliseconds.
|
int |
getFinalY()
Returns where the scroll will end.
|
void |
setFinalY(int newY)
Sets the final position (Y) for this scroller.
|
void |
setFriction(float friction)
The amount of friction applied to flings.
|
int |
timePassed()
Returns the time elapsed since the beginning of the scrolling.
|
public ScrollableScroller(android.content.Context context,
android.view.animation.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 int getDuration()
public final int getCurrY()
public float getCurrVelocity()
public final int getFinalY()
public boolean computeScrollOffset()
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()
public int timePassed()
public void setFinalY(int newY)
newY - The new Y offset as an absolute distance from the origin.
extendDuration(int)
setFinalX(int)