The well-known Android system redraws the activity every 16ms, meaning that your app must do all the logic of the screen refresh within 16ms to reach 60 frames/s. And the user generally see the lag is due to the performance of Android rendering. This blog will cover Android's rendering knowledge.
However, sometimes your program will appear such a situation, if a drawing operation more than 16 seconds spent 24 seconds when the user to see the same picture took 32 seconds instead of 16s, users will feel the lag, this phenomenon we call-drop frame
the rendering mechanism of Android
First of all we want to understand the Android rendering mechanism, Android rendering mainly divided into two components
1.CPU
2.GPU
By both of these together to finish drawing on the screen
How does activity transform a complex UI into a user-readable image and draw it onto the screen?
This is done by rasterization operations, so-called rasterization is the most basic operation of drawing those components such as button,shape,path,string,bitmap. It splits those components into different pixels and displays them in a tacky way, by solving complex XML layout files and markup languages to translate them into images that the user can understand, but this is not directly translated, and XML layout files need to be converted to polygons or textures first in the CPU, It is then passed on to the GPU for rasterization, and for Rasterization, which is related to OpenGL, Rasterization is a particularly time-consuming operation.
simple understanding of the Android rendering process
The CPU enters these instructions to the GPU before the image is drawn this process passes the Opengl-es
That is, when the UI object is drawn on the screen, it needs to be transformed into a polygon-and-pass GPU in the CPU for grid operation.
It takes time for the CPU to convert an object to a polygon. It also takes a while to reduce the number of object conversions and the number of uploads, fortunately the Opengl-es API allows data to be uploaded to the GPU for data storage, and the next time the button is drawn, it is referenced in the CPU's memory.
Therefore, the optimization of rendering performance is to upload data as soon as possible to the GPU as long as possible without modifying the data to save the data
While the Android system has done most of the optimizations, there is also a problem causing the performance impact of –> over-plotting (overdraw)
over-Drawing (overdraw) detection
A pixel on the screen is drawn multiple times within the same frame time
When designing for a more ornate visual effect, it is easy to get caught up in the vicious circle of using more and more cascading components to achieve this visual effect. This can easily lead to a lot of performance problems, in order to get the best performance, we have to minimize the occurrence of overdraw situations.
Follow these steps to turn on show GPU Overrdraw options: Settings, developer options, debug GPU over drawing, display GPU over-drawing
Blue, light green, pale red, crimson represents 4 different degrees of overdraw,
Blue: means Overdraw 1 time times. The pixel was drawn two times. Large blue is still acceptable (if the whole window is blue, you can get rid of one layer).
Green: It means twice times the overdraw. The pixel was drawn three times. Medium-sized green areas are acceptable, but you should try to optimize and reduce them.
Pale red: It means 3 times times overdraw. The pixel is drawn four times, and the small range is acceptable.
Crimson: means Overdraw 4 times times. Pixels are plotted five or more times. This is wrong to fix them.
Our goal is to minimize the red overdraw and see more blue areas.
refine the transition drawing area
Let's take an example to optimize over-drawing, we use the above method to locate the over-drawn place
As can be seen from the picture, the transition drawing from good to bad (blue-green-pink-red), the File Manager transition drawing is very serious, and the setting interface of the transition is within an acceptable range. The following is a file manager as the main analysis object, to see how to optimize the File Manager transition drawing.
from the File Manager diagram, analyze the transition plot area
First of all, look at the top of the Actionbar, compare the settings of the interface Actionbar can be known that the entire file manager has an opaque background, resulting in each drawing, the first to draw this invisible and opaque background. This background is generally applied to the theme of the background, So GPU transitions are plotted to show their bit blue, and this background can be optimized.
In the middle of the content, the bottom is green, indicating that the 2x of the transition to draw, remove the first one we mention the global background, there is a background, that is, 1x of the transition to draw, compared setting know, this background color can be removed.
The easiest to see is these two, we first analyze and optimize the two, and then other optimizations.
refine the transition drawing area
After the location confirmation, we probably identified the area where the transition was drawn, so let's use the tool for verification and view validation.
Open Monitor (both Eclipse and Android studio have a quick open button, DDMS, select Hierarchy View in the top right corner, presumably using
The root node: Phonewindos$decorview is the root node of the entire view, and the only child node is actionbaroverlaylayout, which contains the Actionbar, application, and Smartbar.
Here's how to analyze the areas that need to be modified from the hierarchy view binding code
Remove default Background
The above analysis of the transition drawing area of the first, the entire window there is a background, so a redraw, the background is a system-level redraw, and the subject, that is, the background is actionbaroverlaylayout. This type of transition is easier to solve, In the OnCreate method of the main activity of the File Manager, add
this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
You can remove this invisible theme from the background. The following is the removal of the theme background:
Compared to the pre-optimization of the graph can be found that the background is removed, the less a layer of transition painting. The blue on the Actionbar has disappeared. The contents of the middle change from green to blue
Remove a child control background
The second article of the analysis above says "the middle part of the content, the bottom is green, indicating that the 2x transition is drawn", now the middle section has become blue, but this is a global background, resulting in a large number of red and green after the right view pull over. Continue analyzing hierarchy view to find the view corresponding to the middle view: dragrelativelayout, see source, dragrelativelayout inherit from common control: Slidingmenu, Slidingmenu Composed of Customviewabove and Customviewbehind, the former is the part that can be pulled right and left, the latter is the part that cannot be pulled at the bottom (this can be seen from the Hierarchyview: as shown:
Click Customviewbehind to view its occupied area and you can see that the background is drawn with this view, and open the Customviewbehind code to discover that its constructor contains the following code:
setBackgroundColor(getResources().getColor(R.color.mz_slidingmenu_background_light));
This background is not needed, see the source, this view will be in the Slidingmenu.setmenu, be overwritten, or not see. So this layer of view can be removed. The following is a preview of the background that is removed:
You can see that this layer of background is removed and the transition drawing is reduced a lot.
Next, you can see that the customviewabove is also a background of the transition drawing, viewing hierarchy view's Customviewabove child nodes, you can see that the transition drawing is caused by the ListView. Its ID is: Fileslist, find it in the code, and analyze it. After I remove the setbounds of the OnDraw () function in Partitionitemlayout, the transition drawing is further improved (but the ListItem view color is lighter than before.) , this step optimization needs to be carried out according to the specific situation) below is optimized:
By adjusting, it has been found that drawing optimizes a lot
Introduction to the Android Rendering optimization tool
Optimizing Code with Lint
The use of lint tools is relatively simple, according to the instructions given to make the corresponding changes. Sometimes you need tools to determine if you need to modify them. is a simple example. The arrows indicate that the layout or its parent layout is not required. The specific modification method is to remove the framelayout, the relativelayout is promoted to the root view.
The lint tool also makes changes to the potentially unreasonable or bed code in the codes. More important tips include
1. Variables that are declared but not used
2. Null pointers that may be generated
3. No need to write return,continue
4. Simplified notation for complex code
Simplified notation for 5.for loops: foreach
6. Void of empty sentences
7. Empty if
8. Invalid or unused import
using tracer for OpenGL ES
The Tracer tool is also in Android Device Monitor. Click the tracer for OpenGL es button in the upper-right corner to enter (if you don't have this button, click the Open Perspective button next to it and select Tracer for from the options OpenGL es). First open the Tracer tool, there is no content, click on the top right corner of the two buttons (one is to open the existing Gltrace file, the other is the new Gltrace file). Click the Trace button, the phone will automatically launch the application and start the corresponding activity, when the contents of the phone is fully drawn, you can click the Stop button to generate the Gltrace file. The file opens automatically.
Analysis of the Gltrace file, is optimized after the diagram, compared to the pre-optimization of the graph can be found, the optimization will not draw the default background map and Customviewbehind background.
This is only a frame of the drawing, if more than one operation to generate multiple frames of the drawing trace file, you will find that the two backgrounds will be redrawn multiple times, removed will not only reduce the transition drawing, but also speed up the Gup drawing speed.
Optimized for optimization of Android performance series