Android uses scroller to achieve elastic sliding effect

This example shares the specific code of Android using scroller to realize elastic sliding display for your reference. The specific contents are as follows

scrollTo、scrollBy

These two methods are provided in view to realize sliding, but the sliding effect of these two methods is instantaneous and not smooth enough. How to realize the elastic sliding of view? This is the topic of this blog post. In addition, these two functions slide the contents of the view, not the view itself. For example, for an ordinary view, such as textview, its content is text, and the content of ImageView is drawable object. When sliding with these two methods, the text and drawable object are sliding respectively. For ViewGroup, when sliding with these two methods, it is sliding its child elements. Therefore, if you want to use the scrollto and scrollby methods to achieve the effect of dragging a view (which means that an ordinary view does not contain a ViewGroup), you must package a ViewGroup outside the view.

Scroller class

As mentioned above, when using scrollto and scrollby to slide the view, it is very stiff, not smooth enough, and the natural user experience is not good. Therefore, we need to realize an elastic sliding. How to achieve elastic sliding? There are many methods, but the ideas are consistent, that is, to realize the sliding of a distance, which is divided into several times, each sliding a small segment, asymptotic sliding. This article only introduces one of them, that is, using scroller to realize elastic sliding. Let's take a look at how scroller realizes smooth sliding with examples below?

In line 9, first create a scroller object inside the smoothscrollview. In line 13, the Smoothscroll method is to realize the smooth sliding of smoothscrollview. You can see that to realize the smooth sliding, first call the startscroll method of the scroller in line 18 to set the sliding parameters. This method will be analyzed in the next article. Let's put it first. Then, call the invalidate method on line 20. This method will cause the smoothscrollview to redraw, and then call the computescroll method after calling the draw method. You can see on line 24 that the computescroll method is rewritten here. Therefore, calling the invalidate method will eventually lead to the computescroll method being called. Lines 27 to 29 call the computescrolloffset method of scroller and judge whether the sliding ends. How does computescrolloffset judge the sliding end? Let's also put it in the analysis below. If the sliding is not finished, execute line 33, call scrollto to slide the smoothscrollview to the current destination coordinate, and then call the invalidate method recursively.

The following is an analysis of several methods of scroller:

From the above code, we can see that the startscroll method does not slide at all, and the view only sets the sliding parameters. Next, let's take a look at the computescrolloffset method. If computescrolloffset returns true, it means that the sliding is not over. If false, it means that the sliding is over. Its implementation is as follows: [Java] view plain copy

Seeing this, we should understand how smoothscrollview makes itself slide smoothly? In fact, it is not the scroller that really makes smoothscrollview slide smoothly, but the smoothscrollview itself. Smoothscrollview itself calls its own scrollto method many times and slides one small step at a time to achieve smooth sliding. What the scroller class does is help smoothscrollview calculate the target coordinates to be reached for each small slide, The implementation calls its own scrollto method many times, and the loop body is not used here. It uses the programming technique of "recursive call" invalidate method to call the scrollto method many times, so as to realize smooth sliding.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>