Scroller
open class Scroller
kotlin.Any | |
↳ | android.widget.Scroller |
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(); ... }
Summary
Public constructors | |
---|---|
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!, flywheel: Boolean) Create a Scroller with the specified interpolator. |
Public methods | |
---|---|
open Unit |
Stops the animation. |
open Boolean |
Call this when you want to know the new location. |
open Unit |
extendDuration(extend: Int) Extend the scroll animation. |
open Unit |
fling(startX: Int, startY: Int, velocityX: Int, velocityY: Int, minX: Int, maxX: Int, minY: Int, maxY: Int) Start scrolling based on a fling gesture. |
Unit |
forceFinished(finished: Boolean) Force the finished field to a particular value. |
open Float |
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 |
Returns how long the scroll event will take, in milliseconds. |
Int |
Returns where the scroll will end. |
Int |
Returns where the scroll will end. |
Int |
Returns the start X offset in the scroll. |
Int |
Returns the start Y offset in the scroll. |
Boolean |
Returns whether the scroller has finished scrolling. |
open Unit |
Sets the final position (X) for this scroller. |
open Unit |
Sets the final position (Y) for this scroller. |
Unit |
setFriction(friction: Float) The amount of friction applied to flings. |
open Unit |
startScroll(startX: Int, startY: Int, dx: Int, dy: Int) Start scrolling by providing a starting point and the distance to travel. |
open Unit |
Start scrolling by providing a starting point, the distance to travel, and the duration of the scroll. |
open Int |
Returns the time elapsed since the beginning of the scrolling. |
Public constructors
Scroller
Scroller(context: Context!)
Create a Scroller with the default duration and interpolator.
Scroller
Scroller(
context: Context!,
interpolator: Interpolator!)
Create a Scroller with the specified interpolator. If the interpolator is null, the default (viscous) interpolator will be used. "Flywheel" behavior will be in effect for apps targeting Honeycomb or newer.
Scroller
Scroller(
context: Context!,
interpolator: Interpolator!,
flywheel: Boolean)
Create a Scroller with the specified interpolator. If the interpolator is null, the default (viscous) interpolator will be used. Specify whether or not to support progressive "flywheel" behavior in flinging.
Public methods
abortAnimation
open fun abortAnimation(): Unit
Stops the animation. Contrary to forceFinished(boolean)
, aborting the animating cause the scroller to move to the final x and y position
See Also
computeScrollOffset
open fun computeScrollOffset(): Boolean
Call this when you want to know the new location. If it returns true, the animation is not yet finished.
extendDuration
open fun extendDuration(extend: Int): Unit
Extend the scroll animation. This allows a running animation to scroll further and longer, when used with setFinalX(int)
or setFinalY(int)
.
Parameters | |
---|---|
extend |
Int: Additional time to scroll in milliseconds. |
See Also
fling
open fun fling(
startX: Int,
startY: Int,
velocityX: Int,
velocityY: Int,
minX: Int,
maxX: Int,
minY: Int,
maxY: Int
): Unit
Start scrolling based on a fling gesture. The distance travelled will depend on the initial velocity of the fling.
Parameters | |
---|---|
startX |
Int: Starting point of the scroll (X) |
startY |
Int: Starting point of the scroll (Y) |
velocityX |
Int: Initial velocity of the fling (X) measured in pixels per second. |
velocityY |
Int: Initial velocity of the fling (Y) measured in pixels per second |
minX |
Int: Minimum X value. The scroller will not scroll past this point. |
maxX |
Int: Maximum X value. The scroller will not scroll past this point. |
minY |
Int: Minimum Y value. The scroller will not scroll past this point. |
maxY |
Int: Maximum Y value. The scroller will not scroll past this point. |
forceFinished
fun forceFinished(finished: Boolean): Unit
Force the finished field to a particular value.
Parameters | |
---|---|
finished |
Boolean: The new finished value. |
getCurrVelocity
open fun getCurrVelocity(): Float
Returns the current velocity.
Return | |
---|---|
Float |
The original velocity less the deceleration. Result may be negative. |
getCurrX
fun getCurrX(): Int
Returns the current X offset in the scroll.
Return | |
---|---|
Int |
The new X offset as an absolute distance from the origin. |
getCurrY
fun getCurrY(): Int
Returns the current Y offset in the scroll.
Return | |
---|---|
Int |
The new Y offset as an absolute distance from the origin. |
getDuration
fun getDuration(): Int
Returns how long the scroll event will take, in milliseconds.
Return | |
---|---|
Int |
The duration of the scroll in milliseconds. |
getFinalX
fun getFinalX(): Int
Returns where the scroll will end. Valid only for "fling" scrolls.
Return | |
---|---|
Int |
The final X offset as an absolute distance from the origin. |
getFinalY
fun getFinalY(): Int
Returns where the scroll will end. Valid only for "fling" scrolls.
Return | |
---|---|
Int |
The final Y offset as an absolute distance from the origin. |
getStartX
fun getStartX(): Int
Returns the start X offset in the scroll.
Return | |
---|---|
Int |
The start X offset as an absolute distance from the origin. |
getStartY
fun getStartY(): Int
Returns the start Y offset in the scroll.
Return | |
---|---|
Int |
The start Y offset as an absolute distance from the origin. |
isFinished
fun isFinished(): Boolean
Returns whether the scroller has finished scrolling.
Return | |
---|---|
Boolean |
True if the scroller has finished scrolling, false otherwise. |
setFinalX
open fun setFinalX(newX: Int): Unit
Sets the final position (X) for this scroller.
Parameters | |
---|---|
newX |
Int: The new X offset as an absolute distance from the origin. |
See Also
setFinalY
open fun setFinalY(newY: Int): Unit
Sets the final position (Y) for this scroller.
Parameters | |
---|---|
newY |
Int: The new Y offset as an absolute distance from the origin. |
See Also
setFriction
fun setFriction(friction: Float): Unit
The amount of friction applied to flings. The default value is ViewConfiguration#getScrollFriction
.
Parameters | |
---|---|
friction |
Float: A scalar dimension-less value representing the coefficient of friction. |
startScroll
open fun startScroll(
startX: Int,
startY: Int,
dx: Int,
dy: Int
): Unit
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.
Parameters | |
---|---|
startX |
Int: Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left. |
startY |
Int: Starting vertical scroll offset in pixels. Positive numbers will scroll the content up. |
dx |
Int: Horizontal distance to travel. Positive numbers will scroll the content to the left. |
dy |
Int: Vertical distance to travel. Positive numbers will scroll the content up. |
startScroll
open fun startScroll(
startX: Int,
startY: Int,
dx: Int,
dy: Int,
duration: Int
): Unit
Start scrolling by providing a starting point, the distance to travel, and the duration of the scroll.
Parameters | |
---|---|
startX |
Int: Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left. |
startY |
Int: Starting vertical scroll offset in pixels. Positive numbers will scroll the content up. |
dx |
Int: Horizontal distance to travel. Positive numbers will scroll the content to the left. |
dy |
Int: Vertical distance to travel. Positive numbers will scroll the content up. |
duration |
Int: Duration of the scroll in milliseconds. |
timePassed
open fun timePassed(): Int
Returns the time elapsed since the beginning of the scrolling.
Return | |
---|---|
Int |
The elapsed time in milliseconds. |