Over-plotting (overdraw) means that pixels are drawn several times within a frame of time;
Theoretically, a pixel is optimal to draw once at a time, but because the stacked layout causes some pixels to be drawn more than once, each drawing corresponds to a set of drawing commands and GPU operations for the CPU, so repeated plotting of overlapping invisible elements results in additional calculations. It is necessary to minimize the occurrence of overdraw.
The Android system provides the option to measure Overdraw, in the developer Options-debug GPU over-draw (Show GPU Overdraw), open the option to see the status of the current page overdraw.
Depending on the number of overdraw, different colors will be 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: try to avoid overlapping the drawing of invisible elements
Remove unwanted background resources
- added in theme
android:windowbackground="null" ;
- Set GetWindow () in activity. Setbackgrounddrawable (NULL)
This method should be after Setcontentview (), because GetWindow (). SetBackground (drawable) will talk about drawable settings here Decorview background, The default is 0xff000000, and Setcontentview will initialize the Phonewindow Decorview for the first time;
Segment setting background
Sometimes in order to facilitate the layout to set a whole background, and then set the background for sub-view, here will also cause overlap, if the Sub-view width mach_parent, you can see completely covered the layout of the part, here you can set the background to reduce repainting.
View OnDraw () method
Customize view drawing to avoid overlapping portions of the drawing, you can use
canvas// 裁剪canvascanvas// 判断矩形区域是否相交
Other drawing optimization recommendations
- Avoid allocating memory and creating objects in the OnDraw function, which will cause frequent garbage collection to degrade performance;
Doing these things in the initialization, or the animation gap
- Reduction of invalidate calls
- Keep layout as flat as possible, call Requestlayout () as little as possible, requestlayout will cause the system to traverse the entire view tree to measure and layout, and if the layout is nested complex, there will be performance issues
- If the layout is complex, you can consider customizing the ViewGroup for special handling
Reference
Android Performance Patterns:understanding Overdraw
Android performance Patterns:invalidations, Layouts, and performance
Android Performance Optimization series--understanding Overdraw