preface

Whenever we talk about UI optimizations, rendering-related concepts such as 16ms and 60FTP come to mind, as well as using HierarchyViewer to look at some of the rendering information that UI optimizations need to be understood, but are somewhat difficult to manipulate. UI optimization also has some simple and easy to modify operations, because it is not complex, but often ignored, but the role should not be underestimated.

The body of the

UI optimization should be done from what you can do, not look down on every small change. This article describes optimizations that can be made in everyday development to reduce the level of drawing, mainly by removing redundant backgrounds and the use of some tags.

  1. Remove the default background. When we use some of Android’s built-in themes, the window is given a solid color background by default, which is held by the DecorView. Usually when we write an XML layout, we add a background color or a background image to the entire page. In this case, the Window default background is redundant, but it is still worn by Overdraw, so the Window default background should be removed. Call getWindow().setBackgroundDrawable(null) after setContentView() in onCreate(); Can be removed.

  2. The subview background is set separately. All sub-views are the same background color, it is recommended to set in the root layout, otherwise, it is recommended to set the corresponding background color in the sub-view, which is also more conducive to later maintenance.

  3. The background of the ListView and its Item. Set the background of ListView or RecyclerView and the root layout of its Item. If the color of the Item is the same, set it uniformly in ListView; otherwise, it is recommended to set it separately in the Item.

  4. Setting when displaying the background color. Most of the time, the space between the views needs to display the background color (the background color), and the View color and the background color (the background color) are also different. To save trouble, we usually set the background color directly in the root layout. This can easily lead to overdrawing of the child layout. You can use another View to show the interval and set the corresponding color.

  5. Fragment and Activity background colors. Fragment and Activity double background Fragment and Activity layout, whether to set the background color repeatedly? It is recommended to set the background color in Fragment to avoid inadvertantly setting the background color repeatedly.

  6. RelativeLyout and Linerlayout are reasonable choices. Select Linerlayout and RelativeLyout. If you have the same hierarchy and Linerlayout’s child views do not use the weight attribute, use Linerlayout. Use a RelativeLyout if Linerlayout increases the hierarchy. Try not to use a nested RelativeLayout. Try not to use the weight attribute in all nested linearLayouts. Layout selection to minimize the View tree hierarchy.

  7. Use of merge, include, ViewStub tags. Merge and include tags are usually used together, and viewStubs are placeholders.

  8. ImageView background Settings. Remove the background from ImageView when there is an image loaded, set the background when there is no image, make a judgement when writing code.

  9. ViewPager and Fragment display. When using ViewPager and Fragment displays, Fragement displays are better Inflat than setVisible.

  10. Using a selector when you’re doing a selector, sometimes simplifies things a lot.

  11. Use styles. You can reuse style definitions using styles when multiple components have similar properties.

  12. Define drawable to replace the use of image resources. Reduce memory consumption by defining drawable instead of image resources.

The above 12 items need to be used flexibly, especially the setting of background color, according to the design drawing to apply flexibly, are to reduce excessive drawing as the goal. Simple things are not necessarily useless, optimization is often a struggle with a few seconds, these can be done without tools check, why not check?