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.
Api
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.
Code
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 (); } }
Thecomputescrolloffset method always returns False when Startscroll executes during duration time, but returns true when the animation finishes executing.
When we execute Ontouch or invalidate () or postinvalidate () will result in the execution of this method of Computescroll (). so postinvalidate after execution, will go to tune the Computescroll method, and this method again to tune postinvalidate, so You can constantly call the Scrollto method until the end of the Mscroller animation, of course, the first time, we need to manually call the postinvalidate to call.
direction
I'm the dividing line of the king of the land Tiger.
Android--Scroller