Ext.: http://ipjmc.iteye.com/blog/1615828
The Scroller class in Android is a helper class to implement view smooth scrolling. Usually used in custom view, a private member Mscroller = new Scroller (context) is defined in view. Setting the Mscroller scrolling position does not cause the view to scroll, usually using Mscroller to record/calculate the position of the view scroll, and then rewrite the view's Computescroll () to complete the actual scrolling.
Related APIs are described below
Mscroller.getcurrx ()//gets the position of the Mscroller current horizontal scrollMscroller.getcurry ()//gets the position of the Mscroller current vertical scrollMscroller.getfinalx ()//gets the horizontal position of the Mscroller final stopMscroller.getfinaly ()//gets the vertical position of the Mscroller final stopMscroller.setfinalx (intNEWX)//sets the horizontal position of the Mscroller final stop, without animation effect, jumps directly to the target positionMscroller.setfinaly (intNewy)//set the vertical position of the Mscroller final stop, without animation effect, jump directly to the target position//Scroll, StartX, starty to start scrolling position, dx,dy for scrolling offset, duration for time to complete scrollingMscroller.startscroll (intStartX,intStarty,intDxintDy//Use default finish time 250msMscroller.startscroll (intStartX,intStarty,intDxintDyintduration) Mscroller.computescrolloffset ()//a return value of Boolean,true indicates that scrolling has not been completed and false indicates that scrolling is complete. This is a very important method, usually placed in View.computescroll (), to determine whether scrolling is over or not.
For example, to customize a customview, use Scroller to implement scrolling:
ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;Importandroid.widget.LinearLayout;ImportAndroid.widget.Scroller; Public classCustomViewextendsLinearLayout {Private Static FinalString TAG = "Scroller"; PrivateScroller Mscroller; PublicCustomView (Context context, AttributeSet attrs) {Super(context, attrs); Mscroller=NewScroller (context); } //call this method to scroll to the target location Public voidSmoothscrollto (intFxintFY) { intDX = FX-Mscroller.getfinalx (); intDY = fy-mscroller.getfinaly (); Smoothscrollby (dx, dy); } //call this method to set the relative offset of the scrolling Public voidSmoothscrollby (intDxintdy) { //set the scroll offset of the MscrollerMscroller.startscroll (Mscroller.getfinalx (), mscroller.getfinaly (), DX, DY); Invalidate ();//This must be called invalidate () to ensure that computescroll () will be called, otherwise it will not necessarily refresh the interface, not see the scrolling effect} @Override Public voidComputescroll () {//first determine if Mscroller scrolling is complete if(Mscroller.computescrolloffset ()) {//This calls the view's Scrollto () to complete the actual scrollingScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ()); //You must call this method, or you may not see the scrolling effectpostinvalidate (); } Super. Computescroll (); }}
Android scroller Simple Usage--view scrolling