In depth understanding of scroller’s scrolling principle in Android

Smooth scrolling effect of view

What is the smooth scrolling effect of a view? For example, a view scrolls from one position to another within the specified time. We can use the scroller class to realize uniform scrolling, which can accelerate before decelerating, decelerate before accelerating, and so on, rather than the effect of instantaneous movement, So scroller can help us achieve a lot of sliding effects.

First, let's take a look at the usage of scroller, which can be basically summarized as "trilogy":

1. Create a scroller object, which is usually created in the view constructor:

2. If you override the computescroll() method of view, the following code will not change:

3. Call the startscroll() method. Startx and starty are the coordinate points to start scrolling, and DX and Dy are the corresponding offsets:

The above three steps are the basic usage of scroller.

The next task is to analyze the scroller's scrolling principle.

Before that, we have one more thing to do, that is to understand the principles of scrollto() and scrollby(). I won't repeat the difference between scrollto() and scrollby(). Those who don't understand can Google or Baidu themselves.

The source code of scrollto () is posted below:

After setting up mScrollX and mScrollY, you call onScrollChanged (mScrollX, oldY); The view will be redrawn. This achieves the sliding effect.

Let's take a look at scrollby():

I'm sure you all understand this short code. It turns out that scrollby() calls scrollto() internally. However, the scrollto() / scrollby() scrolls are completed instantly. How can we achieve smooth scrolling.

I wonder if you have such an idea: if we divide the offset to be scrolled into several small offsets, of course, this amount should be large. Then use scrollto() / scrollby() to scroll the offset of small copies each time. In a certain period of time, doesn't it become smooth rolling? Yes, scroller uses this principle to achieve smooth scrolling.

Let's take a look at the source code!

According to the first part of the trilogy, let's take a look at the constructor of scroller:

The main thing to do in the constructor is to specify the interpolator. If no interpolator is specified, the default viscousfluidinterpolator will be used.

Let's look at scroller's startscroll():

We found that startscroll () does not start scrolling, but sets the initial values of a bunch of variables. So what makes view start scrolling? We should focus on the next sentence of startscroll(), invalidate(); On me. We can understand this: first we set up a bunch of initial values in startScroll (), then we call invalidate (). Let the view redraw. Here is another important point. The method computescroll() will be called in draw()!

The source code is too long, so it won't be posted here. For children's shoes you want to see, search the view class for the Boolean draw (canvas, ViewGroup parent, long drawingtime) method. Via ViewGroup The drawchild () method will call the draw () method of the child view. Computescroll () in the view class is an empty method, which needs us to implement:

In the second part of the "trilogy" above, we have implemented computescroll (). First, judge computescrolloffset(), let's take a look at the relevant source code:

The return value of this method is particular. If it returns true, it means that the scroller's sliding has not ended; If false is returned, scroller's sliding is over. Let's look at the internal code: first, calculate the sliding time. If the sliding time is less than the total sliding time, it means that the sliding is not over; Otherwise, it means that the sliding is over. Set the flag mfinished = true. There are two modes before the end of sliding, but these two modes do almost the same thing. They roughly calculate the rolling distances mcurrx and mcurry at this time point according to the time timepassed and interpolator just now. That's mscroller in the second part of the trilogy above getCurrX() ,mScroller. The value of getcurrent().

Then we call the scrollTo () method in the second passage to scroll to the specified point (the mCurrX, mCurrY above). After that, postinvalidate(); is called, Let the view redraw and call computescroll () again to continue the cycle until the view scrolls to the specified position, and the scroller scrolling ends.

In fact, the principle of scroller is relatively easy to understand. Let's clarify our thinking and end today's scroller analysis in the form of a graph:

summary

Well, this article introduces the scrolling principle of scroller in Android. That's the end. If you have any questions, you can leave a message below. I hope the content of this article can help you develop Android.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>