If you like my blog, please follow my Weibo, please click here (http://weibo.com/kifile), thank you
Reprint please indicate the source (http://blog.csdn.net/kifile), thanks again
Original address: http://developer.android.com/training/improving-layouts/optimizing-layout.html
For the next period of time, I will translate some of the official Android documents for performance improvement every day to everyone
Performance optimization of the layout of the article:
[Android performance Optimization series] layout of the passage <include> reuse layout
Off Topic:
Complex layouts can both improve our design and reduce our code efficiency, so don't use the relative layout more.
Here is the text of this session:
################
When you write a layout file, you think that it is wrong to use the basic layout to reach efficient results. You need to know that every control and layout file you add to your app needs to be initialized, arranged, and drawn in three processes. For example, using nested linear layouts can cause your layout hierarchy to become very redundant. In addition, if the Layout_weight parameter is used in a nested linear layout, then each of his sub-views needs to be re-measured two times. Especially when they are used in the ListView and the GridView, they are measured repeatedly.
In this article, you'll learn how to use the Hierarchy Viewer and layoutopt to detect and optimize your layout.
Check your layout
There is a tool called Hierarchyviewer in the Android SDK that can help you analyze your layout while your app is running. Using this tool can help you identify bottlenecks in layout performance.
Hierarchyviewer allows you to select a process from a device or emulator that is currently connected to your computer and then display his layout tree. And he is able to display the time that each control spends in measure,layout and draw, helping you find the problem
For example, figure 1 shows a layout that is used as a ListView item, the left side of which is a picture, and two levels of text on the right, and you should be aware that this layout will be initialized multiple times in the ListView.
The Hierarchyviewer tool stores the location under <sdk>/tools, and when it is opened, the Hierarchyviewer displays a list of current devices, along with all running components. Click Load View Hierarchy to see the layout level of the selected component. For example, figure 2 shows the layout described in Figure 1.
In Figure 2, you can see that there are three layers of layouts, and clicking on each element will show you the time it takes to display each step in the interface as shown in Figure 3. This allows us to clearly understand how much time you spend in the measurement, layout, and rendering stages, and you will know where you should start to optimize him.
The time shown in is, Measure 0.977ms, Layout 0.167ms, Draw 2.717ms
Fix your layout
From the layout information above, we can see that nested linear layouts reduce our layout efficiency, so we can improve performance by flattening the layout. A linear layout can also be made to resemble the above layout, so we can change the layout to use a linear layout, so that the layout becomes two layers, check the layout will find him like this
Now the time it takes to render an item is to measure 0.598ms, Layout 0.110ms, Draw 2.146ms
It may not seem like much of a boost, but you need to know that this item will be used multiple times in the ListView and drawn repeatedly.
In fact, most of the drawing time difference is because we use the layout_weight in the linear layout, he will reduce the layout of the measurement speed, of course, this is only one of the reasons you should use the layout weights carefully.
Using Lint
Typically you can find possible layout optimizations in your layout files by using Lint. Lint has now replaced Layoutopt, and is more efficient, and here are some of Lint's rules.
1. try to use composite pictures , a linear layout if it contains a ImageView and a TextView, then you can use a composite picture to replace
2. Remove the unwanted root node , if a framelayout is the root node of the entire layout, and he does not provide the background, white and other things, then we can use the merge tag to make him more effective
3. Reduce the foliage in the layout , if a layout has no sub-View or background, then he can be removed (and he is invisible) to make the layout more effective
4. Reduce the parental level , if a layout does not have a brother, and he is not ScrollView or root View, and there is no background, then he can be removed directly, his child can be directly moved to his parents ' level
5. Avoid too deep layout levels , multiple nested layout files are not conducive to performance. You might consider using a relative layout or grid layout to improve performance, with the default deepest depth of layout being 10.
The Lint tool has now been incorporated into the Android development tool and is able to run automatically after you make changes to the project code. You can use the buttons on the Eclipse toolbar to open and close his
When using Eclipse, Lint can fix some problems automatically and provide some suggestions, and you can jump to the corresponding layout file directly by clicking on the code. If you are not using Eclipse as your development environment, you can also use Lint with the command line, for more help information, you can see tools.android.com
[Android performance Optimization series] layout to reduce your interface level