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
Java code
- Mscroller.getcurrx () //Get position of mscroller current horizontal scroll
- Mscroller.getcurry () //Get position of mscroller current vertical scroll
- MSCROLLER.GETFINALX () //Get Mscroller final stop horizontal position
- Mscroller.getfinaly () //Get the vertical position of the Mscroller final stop
- MSCROLLER.SETFINALX (int newx) //Set the horizontal position of the Mscroller final stop, no animation effect, jump directly to the target position
- Mscroller.setfinaly (int newy) //Set the vertical position of the Mscroller final stop, no 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 scrolling
- Mscroller.startscroll (int startX, int starty, int dx, int dy) //Use default finish time 250ms /c3>
- Mscroller.startscroll (int startX, int starty, int dx, int dy, int duration)
- Mscroller.computescrolloffset () ///return value boolean,true indicates that scrolling is not complete, false to indicate 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:
Java code
- Import Android.content.Context;
- Import Android.util.AttributeSet;
- Import Android.util.Log;
- Import Android.view.View;
- Import Android.widget.LinearLayout;
- Import Android.widget.Scroller;
- Public class CustomView extends LinearLayout {
- private static final String TAG = "Scroller";
- private Scroller Mscroller;
- Public CustomView (context context, AttributeSet attrs) {
- Super (context, attrs);
- Mscroller = New Scroller (context);
- }
- //Call this method to scroll to the target location
- public void Smoothscrollto (int fx, int fy) {
- int dx = FX-MSCROLLER.GETFINALX ();
- int dy = fy-mscroller.getfinaly ();
- Smoothscrollby (dx, dy);
- }
- //Call this method to set the relative offset of the scrolling
- public void Smoothscrollby (int dx, int dy) {
- //Set the scroll offset of the Mscroller
- Mscroller.startscroll (Mscroller.getfinalx (), mscroller.getfinaly (), DX, DY);
- Invalidate (); //Must call Invalidate () here to ensure that computescroll () will be called, otherwise it will not necessarily refresh the interface, see Scrolling effect
- }
- @Override
- public void Computescroll () {
- //To determine if the Mscroller scroll is complete first
- if (Mscroller.computescrolloffset ()) {
- //This calls the view's Scrollto () to complete the actual scrolling
- ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ());
- //You must call this method, or you may not see the scrolling effect
- Postinvalidate ();
- }
- Super.computescroll ();
- }
- }
Android scroller Simple usage