Android inherits ViewGroup to realize scroll sliding effect

This article gives an example of how Android inherits ViewGroup to realize scroll sliding effect. Share with you for your reference, as follows:

Extensions ViewGroup needs to override the onmeasure and onlayout methods

The onmeasure method is to measure the size required by the ViewGroup and the child views contained.

After executing the above method, execute the onlayout method to set the placement position of the child views.

To realize the scroll sliding effect, you need to detect the sliding rate, that is, you need to know how many pixel values have been slid per unit time, and judge whether scroll slides to the next page or the previous page according to this pixel value.

Android provides us with the velocitytracker class to detect the rate

Use mvelocitytracker = velocitytracker. Obtain(); To initialize

Use mvelocitytracker. Addmovement (event); Add the touch event to the detection. Note that each touch event should be added

Use mvelocitytracker. Computecurrentvelocity (1000); Calculate how many pixels are slid in each unit time. Here, 1000ms is passed in, that is, 1s.

Then use float pxsec = mvelocitytracker. Getxvelocity(); To obtain the pixel value of x-axis sliding, the above method must be executed and will only be called again.

Finally, mvelocitytracker. Recycle(); mVeLocityTracker = null; Recycle this object.

The complete code is:

Many people will think that the sliding of ViewGroup is the credit of scroller. In fact, it is not. The role of scroller here is more like a tool class used to calculate the moving pixels per unit time of X and Y axes. That's all. It has no special ability.

The scrollto and scrollby methods in the ViewGroup really realize the scrolling effect here. Scrollto slides to and scrollby slides.

The startscroll method of the scroller class passes in five parameters, corresponding to the x-axis sliding offset pixel, Y-axis sliding offset pixel, X-axis sliding pixel, Y-axis sliding pixel and the time required for the sliding process.

Look at the source code. If the method does not pass time parameters, it has a default time of 250ms.

After scroller.startScroll, you need to call the invalidate method, then call the draw method of ViewGroup, then call the computeScroll method, call the Scroller.computeScrollOffset () method in the computeScroll method to determine if there is any computation, do not calculate the end to return true, then scrollto method, then postInvalidate (); Method re executes the computescroll method.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial for Android development, summary of animation skills for Android development, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage, summary of Android view skills, summary of Android layout skills, and Android control usage summary

I hope this article will help you in Android programming.

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