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.
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:
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 ();
- }
- }
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";p rivate Scroller mscroller;public CustomView (context context, attributeset at TRS) {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.GETF Inaly (); Smoothscrollby (dx, dy);} Call this method to set the relative offset of the scrolling public void Smoothscrollby (int dx, int dy) {//Set Mscroller's scroll offset mscroller.startscroll ( Mscroller.getfinalx (), mscroller.getfinaly (), DX, dy); invalidate ();//The Invalidate () must be called here to ensure that Computescroll () is called, Otherwise it will not refresh the interface, see the scrolling effect} @Overridepublic void Computescroll () {///first determine if the Mscroller scroll is complete if (Mscroller.computescrolloffset ()) { This calls the view's Scrollto () to complete the actual scrolling Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ()), or the method must be called, otherwise it may not be possible to see the scrolling effect postinValidate ();} Super.computescroll ();}}
- Scrollerdemo.rar (322.3 KB)
Original: http://ipjmc.iteye.com/blog/1615828
Android scroller Simple usage