Android Scroller class and Velocitytracker class

Source: Internet
Author: User

    Scroller class
  1. 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.
  2. 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
  3. 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
  4. 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 (); }}
  5. 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

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.