Android Official Development Document Training Series Chinese version: Optimization for creating a custom view view

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/custom-views/optimizing-view.html

Now that you've completed a well-designed view, you can respond to gestures and transition between states. To avoid view lag, you need to ensure that animations are always performed at 60 frames per second.

Reduce the frequency as much as possible

To make the view smoother, unnecessary code needs to be removed from the frequently called methods.

First, starting with the OnDraw () method, the moving knife in this method will have a clear sense of fluency. In particular, the memory allocation code in the OnDraw () method should be removed because memory allocation causes garbage collection, which may cause the program to pause. You should request memory when the program is initialized or before the animation starts executing, never in the process of running the animation.

In addition to streamlining the OnDraw () method, you should also make sure that you reduce the frequency of calls to these methods. Most of the calls to the OnDraw () method are caused by the invalidate () method, so remove unnecessary invalidate () execution code.

Another very expensive operation is the measurement of the layout. Each time you call Requestlayout (), the Android UI system measures the entire view level to determine how large each view will be. If the dimensions are found to be in conflict, multiple measurements are required. UI designers sometimes need to create a deep level layout of inline ViewGroup to make the UI layout correct. These deep levels of layout hierarchies can cause performance problems. To make the view level as latent as possible.

If you have a slightly more complex UI, consider writing a custom viewgroup to perform such a layout. Unlike the built-in view, your custom view can make assumptions about the size and shape of its sub-view, so you don't have to measure the size of the sub-view. The pie chart example shows how to use ViewGroup as part of a custom view. Pie charts contain some sub-view, but they are definitely not measured. Instead, it sets the dimensions directly for the view through a unique custom layout algorithm.

Android Official Development Document Training Series Chinese version: Optimization for creating a custom view view

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.