Reprint: 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.
Mscroller.getcurrx ()//Get Mscroller Current horizontal scroll position mscroller.getcurry ()//Get Mscroller current vertical scroll position mscroller.getfinalx ()// Gets the horizontal position of the Mscroller final Stop mscroller.getfinaly ()//Gets the vertical position of the Mscroller final stop mscroller.setfinalx (int newx)// Set Mscroller final Stop horizontal position, no animation effect, jump directly to the target position mscroller.setfinaly (int newy)//Set Mscroller final stop vertical position, no animation effect, jump directly to the target location//scroll, StartX, Starty for the beginning of the scrolling position, dx,dy for the scrolling offset, duration for the time to complete the scroll mscroller.startscroll (int startX, int starty, int dx, int dy)// Use default finish time 250msmscroller.startscroll (int startX, int starty, int dx, int dy, int duration) 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 (); }}
- Scrollerdemo.rar (322.3 KB)
- Download number of times: 1517
[Android Custom Controls] Simple usage of Android scroller tool class and Gesturedetector