Android performance optimization series-Understanding Overdraw
Overdraw refers to the number of times that a pixel is drawn within a frame;
Theoretically, it is optimal to draw a pixel only once at a time. However, due to the stacked layout, some pixels are drawn multiple times, each painting will correspond to a set of drawing commands on the CPU and some operations on the GPU, so repeated painting of overlapping invisible elements will produce additional calculations, and Overdraw should be minimized.
The Android system provides the Overdraw measurement option. In the developer option-Debug GPU Overdraw (Show GPU Overdraw), open the option to view the Overdraw status of the current page.
Based on the number of overdraw times, different colors are displayed to differentiate
Transparent = no overdraw blue = 1 layer green = 2 layers light-red = 3 layers dark red = you're doing it wrong Optimization Method
The general principle is to avoid overlapping invisible elements.
Remove unwanted background resources
Add in theme
android:windowbackground="null"; Set getWindow (). setBackgroundDrawable (null) in Activity)
This method must be followed by setContentView () Because getWindow (). setBackground (Drawable) will show the setting of Drawable here to the background of DecorView. The default value is 0xff000000, and setContentView will initialize DecorView of phoneWindow for the first time;
Segment setting background
Sometimes Layout is configured with an overall background for convenience and a background for the sub-View. This also causes overlap. If the sub-View width is mach_parent, we can see that part of Layout is completely overwritten, here, you can set the background to reduce the re-painting.
View onDraw () method
You can use
Canvas. clipRect (); // crops canvascanvas. quickReject (); // determines whether the rectangular area is intersecting.
We recommend that you avoid Memory Allocation and object creation in the onDraw function, which will lead to frequent garbage collection and lower performance;
During initialization or animation clearance, reduce invalidate calls to keep layout flat as much as possible, and call requestLayout () as little as possible. requestLayout will cause the system to traverse the entire View tree and re-Remove measure and layout, if layout Nesting is complex, performance issues also occur. If the layout is complex, you can customize ViewGroup to handle the Reference
Android Performance Patterns: Understanding Overdraw
Android Performance Patterns: Invalidations, Layouts, and Performance