We have discussed how to use HierarchyViewer and Android lint to optimize our program. This article is summative and uses a small example to explain how to optimize the application layout. This example is provided on the android official website. The author is also a translator.
Most developers may think that using the basic layout structure will produce efficient layout performance. In fact, this idea is not completely correct. Each of the controls and la s we add to the application must be initialized, laid out, and drawn. This usually takes time to reduce the display speed. Additionally, nesting multiple LinearLayout instances that use the layout_weight attribute will incur a higher cost because each sub-layout must be measured twice. If this layout is used in ListView or GridView, rendering will take more time.
Next, we use HierarchyViewer and Android lint to check and optimize the layout structure based on a layout example.
Use HierchyViewer
HierchyViewer requires you to select a connected device or a running program in the simulator to display the layout tree. The traffic lights on each block represent its measurement, layout, and drawing performance, helping you identify potential problems. After reading "Android UI Optimization-using HierarchyViewer tool", some readers suggested that HierarchyViewer did not display red, green, and yellow lights and time. How can this problem be solved. When HierarchyViewer is started in the tool directory, no corresponding settings are displayed. However, you can start HierarchyViewer in eclipse to set HierarchyViewer. Window-> Open Perspective-> others-> hierarchyviewer. In Tree View, click the button in three circles. 1:
Figure 1
Figure 1 Figure 2 shows the difference between displaying the draw time and not displaying the draw time:
Figure 2 Figure 3
We start to analyze the item layout of a ListView. 4. An image is displayed on the left of the layout, and two text items are placed on the right. Optimization is especially important when the layout is repeatedly loaded.
Figure 4
Figure 4 hierarchy 5 of the layout file displayed in HierarcheyViewer. The selected LinearLayout shows various performance parameters. 6:
Figure 5 Figure 6
In Figure 5, the view has a three-tier structure and some display red lights and yellow lights, which requires optimization. Figure 6 also shows the drawing time.
The reason for the low layout performance is mainly caused by an embedded LinearLayout. To improve the performance, we use RelativeLayout, replace the shallow and wide flat structure with a deep and narrow tree structure, so that the layout becomes a two-layer structure. The modified layout structure is 7:
Figure 7
At this point, the painting time is reduced, and the red light and yellow light are removed.
Use lint
Android code optimization-using the Android lint tool briefly describes the use of lint. Running the Lint tool in the layout file can identify the layout structures that may be optimized. The Lint tool replaces the Layoutopt tool and has more functions. Some examples of Lint are as follows:
1. Use compound drawables, which contains an ImageView and a TextView in the LineraLayout layout. You can Use compound drawable instead to achieve better performance.
2. Merge root frame. If the root layout is FrameLayout, you can use <merge/> instead. For details, see Android abstract Layout-include, merge, and ViewStub.
3. Useless leaf. layout without sub-layout can be removed.
Useless parent. A layout is not a ScrollView, a root layout, or a background. Only one child node can be deleted.
5. Deep layouts has poor performance if there are too many embedded layouts. Consider using a flat layout such as RelativeLayout and GridLayout. The maximum layout depth is 10 by default.
To use Lint for Android, go to Android code optimization-use Android lint tool.
: