[Android Pro] Scroller Usage Analysis

Source: Internet
Author: User
Tags gety

Reference to:http://blog.csdn.net/a910626/article/details/51548840

What is a. scroller?
  • 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.

  • Scroller is just a calculator that provides interpolation calculations that let you animate the scrolling process, but it's not a UI, or a secondary UI swipe, but simply provides the calculation for the swipe.

  • No matter from the construction method or other methods, as well as the properties of scroller, it does not hold the view, auxiliary viewgroup sliding.

  • Scroller just provides calculations, and that who comes to call Computescroll makes ViewGroup swipe

  • Computescroll is not to let viewgroup sliding, really let viewgroup sliding is Scrollto,scrollby. The function of Computescroll is to calculate how the ViewGroup slides. And the computescroll is called by draw.

  • Computescroll and Scroller are calculations, what's the relationship between them? There's no direct relationship. Computescroll and scroller if fly Latin America relations, that is computescroll can refer to scroller calculation results to affect the Scrollto,scrollby, so that the sliding changes. That is, Scroller does not call Computescroll, but Computescroll calls Scroller.

  • When sliding, how to make the calculation of scroller continuous? This asked when to call Computescroll, as mentioned above Computescroll call Scroller, as long as computescroll call continuous, scroller will be continuous, essentially Computescroll continuity and Invalidate method control, Scrollto,scrollby will call invalidate, and invalidate back to trigger draw, so computescroll is continuously called, in the synthesis, Scroller will also be called continuously, unless invalidate stops calling.

  • How does Computescroll align with the scroller call process? Computescroll reference scroller affect Scrollto,scrollby, in essence, in order to not repeat the impact of Scrollto,scrollby, then scroller must terminate the calculation Currx,curry. To know whether the calculation is terminated, you need to pass Mscroller.computescrolloffset ()

Two. What is the knowledge application scenario? What problems can be solved?

When we realize the requirements, we often encounter the view sliding situation, and the Scrollto and Scrollby methods can realize the sliding of the view, but the effect is instantaneous, the user experience is not good, We can use Scroller or Smoothscrollto (which is also implemented internally by Scroller) to achieve smooth moving results. Common in Custom view.

Three. Common APIs for this class?
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. 
Four. What is the general usage?

Scroller basic usage is still relatively simple, mainly can be divided into the following several steps:
1. Create an instance of Scroller
2. Call the Startscroll () method to initialize the scrolling data and refresh the interface
3. Rewrite the Computescroll () method and complete the smooth scrolling logic inside it

Five. Where is the difficulty when using?

The difficulty in using is the Startscroll parameter meaning, and then gives the appropriate value according to the meaning of the parameter, because this process involves the calculation of coordinates in Android, so it is more complicated.

       // void Android.widget.Scroller.startScroll (int startX, int starty, int dx, int dy, int duration)         // The first parameter is the x-coordinate value of the        start move, // The second is the y-coordinate value of the        start movement, // The Third fourth parameter is the coordinate value to move to a point-the initial coordinate value, that is, the moving distance        value // duration, of course, is the time to perform the move. 
Six. One case: from "Android Heroes"
 Public classScrollerdragviewextendsView {PrivateScroller Mscroller; Private intMlastx; Private intMlasty;  PublicScrollerdragview (Context context) {Super(context);    Initview (context); }     PublicScrollerdragview (Context context, AttributeSet attrs) {Super(context, attrs);    Initview (context); }    Private voidInitview (Context context) {SetBackgroundColor (Color.Blue); Mscroller=NewScroller (context); } @Override Public Booleanontouchevent (Motionevent event) {intx = (int) Event.getx (); inty = (int) event.gety (); Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:mLastX= (int) Event.getx (); Mlasty= (int) event.gety ();  Break;  CaseMotionevent.action_move:intOffsetX = x-Mlastx; intOffsetY = y-Mlasty; //seemingly using getparent, you no longer need to reset x, y coordinates.(View) getParent ()). Scrollby (-offsetx,-OffsetY);  Break;  CaseMotionEvent.ACTION_UP:View ViewGroup=(View) getParent (); Mscroller.startscroll (Viewgroup.getscrollx (), viewgroup.getscrolly (),-viewgroup.getscrollx (),-viewgroup.getscrolly ());                Invalidate ();  Break; }        return true; }    /*** There is no need for special understanding here, as long as the use of scroller, here are the same*/@Override Public voidComputescroll () {Super. Computescroll (); if(Mscroller.computescrolloffset ()) {(View) getParent ()). ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry (            ));        Invalidate (); }    }}

 

[Android Pro] Scroller Usage Analysis

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.