[Android performance optimization series] layout to reduce your interface level
In the next period of time, I will translate some official Android documents about performance improvement every day for you.
Performance Optimization Layout:
[Android performance optimization series] Layout Reuse Layout
Digress:
Complex layout not only increases the design difficulty, but also reduces the code efficiency.
The following is the body of this article:
################
When writing layout files, you will think that as long as you use the basic layout to achieve efficient results, this is actually wrong. You need to know that every control and layout file you add to your application must be initialized, arranged and drawn. For example, nested linear la S may cause redundant la。 S. In addition, if the layout_weight parameter is used in the nested linear layout, each of his child views needs to be measured twice. Especially when they are used in ListView and GridView, they will be measured repeatedly.
In this article, you will learn how to use Hierarchy Viewer and Layoutopt to detect and optimize your layout.
Check your layout
In the Android SDK, there is a tool called HierarchyViewer that helps you analyze your layout while the application is running. This tool helps you discover bottlenecks in layout performance.
HierarchyViewer allows you to select a process from the device or simulator currently connected to your computer, and then display its layout tree. In addition, it can display the time spent by each control in measure, layout, and draw to help you discover problems.
For example, figure 1 shows a layout used as a ListView item. The layout is an image on the left and two layers of text on the right. You should be clear that the layout will be initialized multiple times in ListView.
The Hierarchyviewer tool is stored in /Tools, when it is turned on, a list of current devices and all running components will be displayed in HierarchyViewer. Click Load View Hierarchy to View the layout level of the selected component. For example, Figure 2 shows the layout in Figure 1.
In Figure 2, you can see that there is a three-layer layout. Clicking on each element will show the time consumed by each step in the interface process as shown in figure 3. This allows us to clearly understand the time you spent in the measurement, layout, and rendering phases. You will also know where to optimize it.
The displayed time is 0.977 ms, the layout is 0.167 ms, and the plot is 2.717 ms.
Correct Your Layout
From the above layout information, we can find that nested linear layout reduces our layout efficiency, so we can improve the performance by flattening the layout. A linear layout can also be made into a layout similar to the preceding layout. Therefore, we can change the layout to a linear layout, so that the layout will become two layers, after checking the layout, you will find that it is like this.
The time taken to render an item is 0.598 ms, the layout is 0.110 ms, and the rendering is 2.146 ms.
This may not seem much improved, but you need to know that this Item will be used multiple times in listView and drawn repeatedly.
In fact, the difference in drawing time is because layout_weight is used in linear layout_weight, which reduces the measurement speed of the layout. Of course, this is only one of the reasons you should exercise caution when using the layout weight.
Use Lint
You can usually use Lint in your layout file to find possible layout optimizations. Lint has now replaced Layoutopt and is more efficient. Below are some of the Lint rules
1.Try to use composite imagesIf a linear layout contains an ImageView and a TextView, you can replace it with a composite image.
2. Remove unnecessary root nodesIf a FrameLayout is the root node of the entire layout and does not provide background, white, or other things, we can use the merge tag to make it more effective.
3. Reduce the branches and leaves in the layoutIf a layout has no sub-View or background, it can be removed (and it is invisible) to make the layout more effective.
4. Reduce parent levelIf a layout has no brothers and is not a ScrollView or root View and has no background, it can be removed directly, his child can be moved directly to the level of his parents.
5. Avoid a deep layout levelMultiple nested layout files are not conducive to performance. You can consider improving performance through relative layout or grid layout. The default deepest layout depth is 10.
Currently, the Lint tool has been integrated into the Android development tool. It can automatically run after you make changes to the project code. You can open or close the tool through the buttons on the Eclipse toolbar.
When using Eclipse, Lint can automatically fix some problems and provide some suggestions. You can also directly jump to the corresponding layout file by clicking the code. If you do not use Eclipse as the development environment, you can also use Lint through the command line. For more help information, see tools.android.com.