Android Custom Control Series----Scroller class description

Source: Internet
Author: User

Said in front of the words:Why should you say scroller this class? What the hell is this class for? If you look at a ListView type of control then you will surely find that there is a Sroller class in it, in fact, it is to assist in recording and calculating the distance and velocity of our sliding. So that we can easily do some sliding and rebound animations when customizing the control, Why? Because the Sroller class has been calculated for you well. Class analysis
public class Scroller {private int mmode;    private int mstartx;    private int mstarty;    private int mfinalx;    private int mfinaly;    private int Mminx;    private int Mmaxx;    private int mminy;  private int Mmaxy, .../** * Call this is the new location of want to know.     If it returns True, * The animation is not yet finished.        */Public Boolean computescrolloffset () {if (mfinished) {return false;            } int timepassed = (int) (Animationutils.currentanimationtimemillis ()-mstarttime); if (timepassed < mduration) {switch (mmode) {case Scroll_mode:float x = Timepas                    sed * mdurationreciprocal;                 if (Minterpolator = = null) x = Viscousfluid (x);                    else x = minterpolator.getinterpolation (x);                Mcurrx = Mstartx + math.round (x * mdeltax); Mcurry = Mstarty + math.round (x * mdeltay);            Break                Case fling_mode:final Float t = (float) timepassed/mduration;                Final int index = (int) (Nb_samples * t);                float Distancecoef = 1.f;                float Velocitycoef = 0.f;                    if (Index < nb_samples) {final float T_inf = (float) index/nb_samples;                    Final float T_sup = (float) (index + 1)/nb_samples;                    Final float d_inf = Spline_position[index];                    Final float D_sup = spline_position[index + 1];                    Velocitycoef = (d_sup-d_inf)/(T_sup-t_inf);                Distancecoef = D_inf + (t-t_inf) * VELOCITYCOEF;                                } mcurrvelocity = Velocitycoef * mdistance/mduration * 1000.0f;                Mcurrx = Mstartx + Math.Round (DISTANCECOEF * (MFINALX-MSTARTX)); Pin to Mminx <= mcurrx <= Mmaxx mcurrx = Math.min (Mcurrx, MMaxX);                                Mcurrx = Math.max (Mcurrx, Mminx);                Mcurry = Mstarty + Math.Round (DISTANCECOEF * (Mfinaly-mstarty));                Pin to Mminy <= mcurry <= mmaxy mcurry = Math.min (Mcurry, Mmaxy);                Mcurry = Math.max (Mcurry, Mminy);                if (Mcurrx = = Mfinalx && Mcurry = = mfinaly) {mfinished = true;            } break;            }} else {mcurrx = Mfinalx;            Mcurry = Mfinaly;        Mfinished = true;    } return true;        } public void Startscroll (int startX, int starty, int dx, int dy, int duration) {mmode = Scroll_mode;        Mfinished = false;        mduration = Duration;        Mstarttime = Animationutils.currentanimationtimemillis ();        Mstartx = StartX;        Mstarty = Starty;        MFINALX = StartX + dx;        Mfinaly = Starty + dy;        Mdeltax = DX;        Mdeltay = dy;Mdurationreciprocal = 1.0f/(float) mduration; }/** * Start scrolling based on a fling gesture.     The distance travelled would * depend on the initial velocity of the fling. * * @param StartX starting point of the scroll (X) * @param starty starting point of the scroll (Y) * @param     Velocityx Initial Velocity of the Fling (X) measured in pixels per * second. * @param velocityy Initial velocity of the Fling (Y) measured in pixels per * Second * @param minX Minimum X value.     The scroller won't scroll past this * point. * @param maxX Maximum X value.     The scroller won't scroll past this * point. * @param miny Minimum Y value.     The scroller won't scroll past this * point. * @param maxy Maximum Y value.     The scroller won't scroll past this * point. */public void Fling (int startX, int starty, int velocityx, int velocityy, int minX, int maxX, int miny, int Maxy) {//Continue a scroll or fling in progress if (Mflywheel &&!mfinished) {Flo            At oldvel = Getcurrvelocity ();            float dx = (float) (MFINALX-MSTARTX);            float dy = (float) (mfinaly-mstarty);            float Hyp = floatmath.sqrt (dx * dx + dy * dy);            float ndx = Dx/hyp;            float Ndy = Dy/hyp;            float Oldvelocityx = ndx * oldvel;            float Oldvelocityy = Ndy * oldvel; if (Math.signum (velocityx) = = Math.signum (Oldvelocityx) && math.signum (velocityy) = = Math.signu                M (oldvelocityy)) {Velocityx + = Oldvelocityx;            Velocityy + = Oldvelocityy;        }} mmode = Fling_mode;        Mfinished = false;             Float velocity = floatmath.sqrt (Velocityx * Velocityx + velocityy * velocityy);        mvelocity = velocity;        mduration = getsplineflingduration (velocity); Mstarttime = Animationutils.currentanImationtimemillis ();        Mstartx = StartX;        Mstarty = Starty; float COEFFX = Velocity = = 0?        1.0f:velocityx/velocity; float Coeffy = Velocity = = 0?        1.0f:velocityy/velocity;        Double totaldistance = getsplineflingdistance (velocity);                mdistance = (int) (totaldistance * math.signum (velocity));        Mminx = MinX;        Mmaxx = MaxX;        Mminy = Miny;        Mmaxy = Maxy;        Mfinalx = StartX + (int) math.round (totaldistance * coeffx);        Pin to Mminx <= mfinalx <= Mmaxx mfinalx = Math.min (Mfinalx, Mmaxx);                Mfinalx = Math.max (Mfinalx, Mminx);        Mfinaly = starty + (int) math.round (totaldistance * coeffy);        Pin to Mminy <= mfinaly <= mmaxy mfinaly = Math.min (Mfinaly, Mmaxy);    Mfinaly = Math.max (Mfinaly, Mminy); }/** * Stops the animation. Contrary to {@link #forceFinished (Boolean)}, * aborting the animating cause the scroller to move to the final X and y * position * * @see #forceFinished (Boolean) */public void abortanimation () {Mcurrx = MFin        AlX;        Mcurry = Mfinaly;    Mfinished = true; }....
The above I put out of the class method are commonly used several methods, here I come to explain the role of each method and usage, first of all we still see Computescrolloffset () This method, we see its comments/**
Want to know the new location. If it returns true, call this method when you want a new location. In fact, in this class has given us a good example, I also posted here to see what the hell is it?
* <p>to track the changing positions of the X/y coordinates, use * {@link #computeScrollOffset}. The method returns a Boolean to indicate * whether the scroller is finished. If it isn ' t, it means that a fling or * programmatic pan operation are still in progress.  Can use this method to * find the current offsets of the x and Y coordinates, for example:</p> * * <pre>if (Mscroller.computescrolloffset ()) {*     //Get current x and y positions *     int currx = MSCROLLER.GETCURRX (); *     int currY = Mscroller.getcurry (); *    ... *}</pre>
Google's official comments are still very good, I will not say much, and then look at the next method. Startscroll () When should this method be used? And where should I use it? In fact, we need to do a sliding effect, we need to call this method to help us calculate our current position at all times. So we don't have to figure out what the current speed is and where it's going to be. Here's an example of what the authorities are going to do.
<pre> private Scroller Mscroller = new Scroller (context); * ... * public void zoomin () {*     //Revert any animation currently in progress *     mscroller.forcefinished (true); *<  c3/>//Start Scrolling by providing a starting point and *     //The distance-travel *     mscroller.startscroll (0, 0, 100, 0); *     //Invalidate to request a redraw *     Invalidate (); *}</pre>

you can see the code above, the comments are nice, right? Note that the last line invalidate this time requires us to manually initiate a refresh request, otherwise the result will be different from what you think. Next is the fling () method, which we can read from its annotations/** * Start scrolling based on a fling gesture. The distance travelled will * depend on the initial velocity of the fling. Depending on your gesture to slide a distance depending on your initial speed, then when should this function be called? So I'm going to tell you that the ListView is not a quick swipe when you see the ListView view quickly slide some entries before it stops, that is, when we quickly swipe the screen to call this function. From the beginning of the next chapter began to talk about the actual combat.

Android Custom Control Series----Scroller class description

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.