Scroller class
- The Scroller class encapsulates scrolling, calculates the position of the view scroll, sets the length of time the scroll occurs, and returns true if the Computescrolloffset function will return false at the end of the scroll.
- Common functions:
- public void Startscroll (int startx,int starty,int dx,int dy,int Duration): Sets the starting coordinates of the scroll, the distance between the horizontal and vertical axes, and the time of scrolling. Note that dx<0 is scrolling right, dy<0 is scrolling down
- public boolean Computescrolloffset (): Returns true to indicate that scrolling is not complete, false to indicate that scrolling is complete, and that the function is usually placed in the View.computescroll function to determine whether scrolling is over. Always returns false within duration time
- PUBILC final int Getcurrx (): The distance from the horizontal axis relative to the start point of the scroll, which is relative to the STARTX, note that it is not relative to the upper-left corner of the screen. This value changes continuously when the scroll is not completed.
- Public final int Getfinalx (): Returns the scrolled distance, which is the value of DX. Scroll right to return negative values
- View scrolling-related functions:
- public void Computescroll (): Is an empty function of the view class that invokes the child's Computescroll method when the parent container re-paints its own child
- public void scrollTo (int x,int y): Scrolls the view to the specified position, which is the distance from the initial position of the view.
- public void Scrollby (int x,int y): The distance to scroll the view, to the right and down is negative
- Use the Scroller class to customize an instance of the ViewGroup:
Public classMyviewgroupextendsLinearLayout {Private BooleanRecover=true; Scroller Mscroller=NULL; PublicMyviewgroup (Context context, AttributeSet attrs) {Super(context, attrs); Mscroller=NewScroller (context); }//functions that actually occur scrolling@Override Public voidComputescroll () {if(Mscroller.computescrolloffset ()) {intdeltx=Mscroller.getcurrx (); intdelty=Mscroller.getcurry (); ScrollTo (Deltx, delty); Postinvalidate (); } } Public voidBeginscroll () {if(!recover) {Mscroller.startscroll (0, 0, 0, 0, 10000); Recover=true; } Else{Mscroller.startscroll (0, 0,-100,-100, 10000); Recover=false; } invalidate (); }}
- Velocitytracker class: Used to track the speed of touch-screen events, usually the steps to use Velocitytracker are as follows:
- Static Velocitytracker obtain (): Gets a Velocitytracker instance
- void Addmovement (Motionevent event): Add motion event to Velocitytracker instance
- void computecurrentvelocity (int units): Calculates the current speed, units is used to set the speed unit, units to 1 for how many pixels per millisecond, and units to 1000 to indicate how many pixels per second
- Float getxvelocity (): Calculates the velocity along the horizontal axis, getyvelocity gets the vertical direction
Android Scroller class and Velocitytracker class