An introduction to the Scroller.startscroll () method of Android development

Source: Internet
Author: User

The previous article said that you can use Scrollby and scrollto to move view, where a problem arises, adding a view in the upper-left corner of the screen (0,0) where he calls

Scrollto ( -300,-300), will immediately jump over, appear very abrupt, how to let him gently row over it? You can use the method in this article.


How to use:

int startx;//The starting point of the sliding action x coordinate
The starting point y-coordinate of the int starty;//sliding action
int DX; X-axis offset to left negative, positive to right (that is, negative to right, positive to left)
int dy; Y-axis offset to left negative, positive to right (that is, negative to right, positive to left)
int duration;//time, defaults to 250ms
Note: The dx and dy are similar to the Scrollby in the previous article, and they are all moved to which place, not to which point.
Scroller mscroller=new scroller (context);
Mscroller.startscroll (Startx,starty,dx,dy);
Invalidate ()//

In addition to the above code, you have to rewrite the view's Computescroll method:

Here my understanding is: to judge a phase of sliding completion, call the Scrollto method to achieve real mobile

@Override
public void Computescroll () {
if (Mscroller.computescrolloffset ()) {
Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
Postinvalidate ();
}
Super.computescroll ();
}

Maybe we still have a little understanding, I'd like to paint it:

A view, from the top 300px, from the left 300px, that is, his coordinates (300,300);

So this time, we want him to move slowly from the current position to (0,0), and take 1 seconds to write:

Mscroller.startscroll (-300,-300, 300, 300,1000);

Suppose we want it to move from (300,300) to (100,100):

Mscroller.startscroll (-300,-300, 200, 200,5000);

Suppose we want it to move from (300,300) to (200,200):

Mscroller.startscroll (-300,-300, 100, 100,5000);

It just moves a certain distance, so how do we make it move to a fixed point?

We can do this: suppose we want to move to (100,100):


Mscroller.startscroll (-300,-300, 0, 0, 500);
MSCROLLER.SETFINALX (100);
Mscroller.setfinaly (100);
Invalidate ();

Why do I write like this? There is a basis, look at the source of Startscroll:

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;
}

which

MFINALX = startx + dx;
Mfinaly = Starty + dy;

is to add a few values that we're passing in, meaning that the dx,dy we're moving is the offset that we move, bound by the starting values,

So we can control Mfinalx and mfinaly on our own.

Finally, explain the Computescroll method:


@Override
public void Computescroll () {
if (Mscroller.computescrolloffset ()) {
Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
Postinvalidate ();
}
Super.computescroll ();
}

First, take the example above:

Mscroller.startscroll (-300,-300, 300, 300,1000);
at (300,300) point, spents one second, move to (0,0), we think about it, in a second to complete the operation, assuming that we are divided into 100 parts, that is, a part of 10ms,

Then in the first 10ms, Mscrollerd's Getcurrx () is -300+3=-270,getcurrx the same, that is, after the first 10ms, view needs to move to the left 3px, this time computescroll called, Let's first determine if the first phase of Mscroller is complete and, assuming it is done, call the Scrollto () method.

This passes through 100 parts, which, in our view, are slowly moving past.

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.