Overdraw of Android UI performance optimization

Reprinted from: https://blog.csdn.net/u011693517/article/details/49155965

What is Overdraw?

Overdraw means that a pixel on the screen is drawn multiple times in the same frame. In a multi-layer layout structure, invisible parts are also drawn. For example, a white page with a button. The system first draws the white background, then draws the button on the white background, and finally draws the button content on the button background. Then the button and button content cause Overdraw. 
In fact, Overdraw is inevitable. What we need to do is to check the places where there are too many Overdraws and delete the useless drawing.

How to check Overdraw?

Check the Show GPU overdraw option in the developer tools and observe the Overdraw situation on the UI.


screenshot


The tool will be drawn on the screen in different colors to identify the overdraw situation of this block of UI.


screenshot


If it is a transparent color, it means that it is only drawn once, and there is no Overdraw. 
All we have to do is reduce the large red areas and make them blue or even transparent. For deep red, it is necessary to think about what went wrong and resolutely optimize it.

Optimize the logistics details page of Cainiao Baobao

First open the Show GPU overdraw option and observe the situation.


screenshot


Pretty scary, almost all dark red.

Analyze Overdraw Causes

Layout structure: Based on ListView display, express information, item information, and small-piece member information are encapsulated in the same View, added to ListView as HeaderView, and the item of ListView displays logistics tracking information.


screenshot

Drawing level:

Layer 1: Window background 
Layer 2: Fragment container background in Activity 
Layer 3: ListView background in Fragment 
Layer 4: Header background of ListView and item background of logistics information 
Layer 5: Express information, item information, and small member information Background and logistics information item content 
Layer 6+: courier information, item information, small piece information content

start optimizing

The first step is to delete the default background of the window

The Android system provides a default background, which is held by DecorView, but usually, we provide a custom color for the full screen, so this background is useless, but it will bring an Overdraw performance loss. 
In the onCreate method of Activity, call getWindow().setBackgroundDrawable(null) to delete the default background. 
Note that this method should be called after setContentView. The reason can be seen by analyzing the source code: 
setContentView calls setContentView of PhoneWindow and then installDecor. Finally set the background by the following code:

if (mDecor.getBackground() == null &&mBackgroundFallbackResource != 0) {
    mDecor.setBackgroundFallback(mBackgroundFallbackResource);
}
  • 1
  • 2
  • 3

And getWindow().setBackgroundDrawable(null) will also call the following methods of PhoneWindow:

public final void setBackgroundDrawable(Drawable drawable) {
    if (drawable != mBackgroundDrawable || mBackgroundResource != 0) {
        mBackgroundResource = 0;
        mBackgroundDrawable = drawable;
        if (mDecor != null) {
            mDecor.setWindowBackground(drawable);
        }
        if (mBackgroundFallbackResource != 0) {
            mDecor.setBackgroundFallback(drawable != null ? 0 : mBackgroundFallbackResource);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

It can be seen that if getWindow().setBackgroundDrawable(null) is called first, setContentView will set the background color again. 
So getWindow().setBackgroundDrawable(null) should be called after setContentView.

The second step is to delete the background that is repeatedly set in the layout

The entire layout is based on the full-screen display of the ListView, so it conflicts with the fragment container background in the Activity. You can keep one of them. Here, you can choose to delete the background of the ListView, and the container will set the overall background.


screenshot


The optimization effect is beginning to bear fruit. The full screen of red is gone, replaced by large patches of green and blue, small patches of light red and very little dark red.

The third step, optimize the HeaderView

From the UI design point of view, several blocks of the HeaderView have a white background and are separated by gray in the middle. 
The original implementation scheme is that the root view sets a gray background, and the sub view uses a margin to expose the background color of the root view. As shown below:


screenshot


There is room for optimization here. The double-layer structure can be changed to a single-layer, and the division between blocks can be done with a view with a gray background. The modified structure is as follows:


screenshot


Through the optimization of the layout structure, the HeaderView can be reduced one more Overdraw.

The fourth step, fine-grained optimization

Observing the situation after the second step of optimization, it can be found that the item information TextView and the express icon ImageView still have dark red. 
Check the layout file and find that the TextView has the same background color as the parent view, which is very simple, just delete it. 
The express icon is composed of two ImageViews, one displays the express icon and the other adds a border to the icon. I merged two ImageViews, displayed the icon through the src attribute, set the border with the background attribute, and added a 1px padding to display the border. 
The item icon uses a custom ImageView, and there is a bug in the above optimization method, which will not be dealt with for the time being. 
At this point, the optimization work is completed, and the following figure shows the effect after optimization.


screenshot


It can be seen that the entire screen is basically blue, a small amount of green and a small area of ​​light red, which basically achieves the optimization effect.

Finally, summarize the optimization steps

  1. Remove the window default background.
  2. Remove duplicated backgrounds in multi-layer layout structures
  3. Optimize the layout and reduce the layout hierarchy.
  4. Check the components, remove the redundancies.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326292487&siteId=291194637