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