Android scroller Simple usage

Source: Internet
Author: User

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
  1. Mscroller.getcurrx () //Get position of mscroller current horizontal scroll
  2. Mscroller.getcurry () //Get position of mscroller current vertical scroll
  3. MSCROLLER.GETFINALX () //Get Mscroller final stop horizontal position
  4. Mscroller.getfinaly () //Get the vertical position of the Mscroller final stop
  5. MSCROLLER.SETFINALX (int newx) //Set the horizontal position of the Mscroller final stop, no animation effect, jump directly to the target position
  6. Mscroller.setfinaly (int newy) //Set the vertical position of the Mscroller final stop, no animation effect, jump directly to the target position
  7. Scroll, StartX, starty to start scrolling position, dx,dy for scrolling offset, duration for time to complete scrolling
  8. Mscroller.startscroll (int startX, int starty, int dx, int dy) //Use default finish time 250ms /c3>
  9. Mscroller.startscroll (int startX, int starty, int dx, int dy, int duration)
  10. 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
  1. Import Android.content.Context;
  2. Import Android.util.AttributeSet;
  3. Import Android.util.Log;
  4. Import Android.view.View;
  5. Import Android.widget.LinearLayout;
  6. Import Android.widget.Scroller;
  7. Public class CustomView extends LinearLayout {
  8. private static final String TAG = "Scroller";
  9. private Scroller Mscroller;
  10. Public CustomView (context context, AttributeSet attrs) {
  11. Super (context, attrs);
  12. Mscroller = New Scroller (context);
  13. }
  14. //Call this method to scroll to the target location
  15. public void Smoothscrollto (int fx, int fy) {
  16. int dx = FX-MSCROLLER.GETFINALX ();
  17. int dy = fy-mscroller.getfinaly ();
  18. Smoothscrollby (dx, dy);
  19. }
  20. //Call this method to set the relative offset of the scrolling
  21. public void Smoothscrollby (int dx, int dy) {
  22. //Set the scroll offset of the Mscroller
  23. Mscroller.startscroll (Mscroller.getfinalx (), mscroller.getfinaly (), DX, DY);
  24. Invalidate (); //Must call Invalidate () here to ensure that computescroll () will be called, otherwise it will not necessarily refresh the interface, see Scrolling effect
  25. }
  26. @Override
  27. public void Computescroll () {
  28. //To determine if the Mscroller scroll is complete first
  29. if (Mscroller.computescrolloffset ()) {
  30. //This calls the view's Scrollto () to complete the actual scrolling
  31. ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ());
  32. //You must call this method, or you may not see the scrolling effect
  33. Postinvalidate ();
  34. }
  35. Super.computescroll ();
  36. }
  37. }

Android scroller Simple usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.