"Go" Android performance optimizations-over-drawing solutions

Source: Internet
Author: User
Tags set background

Reprint please indicate the source: http://blog.csdn.net/a740169405/article/details/53896497 over-drawing:

A pixel on the screen is repeatedly drawn in one frame, or over-drawn.
A number of cards fall together, but only the first card is completely visible. The cards behind are only partially visible. However, the Android system draws the underlying cards when it is drawn, and then draws the upper cards. But in fact, the parts that are not visible to the underlying cards are not required to be drawn, only the visible ones need to be drawn.

Depending on the over-drawn layer can be divided into:
-No over-drawing (one pixel is only drawn once)
-Over-drawn X1 (one pixel was drawn two times)
-Over-drawn x2 (one pixel was drawn three times)
-Over-drawn x3 (one pixel was drawn four times)
-Over-draw x4+ (one pixel has been drawn more than five times)

To view the over-rendering of your app:

Method one: Turn on GPU over-drawing debugging with developer options
The developer options for Android phones have the option "debug GPU Over-drawing":

After the point is opened, select Show over Paint area:

Method Two: Turn on GPU over-drawing debugging via ADB command
Of course, if you have trouble getting into the system setup every time, you can use the ADB command to turn it on and off:
Turn on "Debug GPU Over-drawing":

adb shell setprop debug.hwui.overdraw show
    • 1

To turn off debug GPU over-painting:

debug.hwui.overdraw false
    • 1

You may need to restart the app you are currently developing after executing the command.

Color and over-drawing:
    • Primary color: No over-drawing
    • Blue: 1 times over-drawn
    • Green: 2 times over-drawn
    • Pink: 3 times over-drawn
    • Red: over 4 times and over drawing

In the development of peacetime, if there is more than pink and over-drawing situation. Description of excessive drawing and very serious. needs to be optimized.

Optimize over-draw:

1. Remove the default background color from the activity:
View the theme theme in the Android source code, as follows:

<style name="Theme">    ...    <!-- Window attributes -->    <item name="windowBackground">@drawable/screen_background_selector_dark</item>    ...</style>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

That is to say, inherit the style of theme, by default, a new activity is a background. Under normal circumstances, many interfaces do not require a background.

The following is the homepage of Huawei's weather app, we can see the text part and the icon part are green, said that the surface is already the third layer over-drawn, the background of the weather map is a layer, the text is a layer, normally should only two layers, that is, the text and the icon should be blue. Then this extra layer should be the background color of the activity itself. Which is set in theme.

We just need to remove the background color in our own apptheme:

<style name="AppTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:windowBackground">@null</item></style>
    • 1
    • 2
    • 3

Or in the OnCreate method of activity:

getWindow().setBackgroundDrawable(null);
    • 1

2. Use the Cliprect and Clippath methods of the canvas to limit the drawing area of the view
An activity to have a canvas, that is, the canvas, the concept of a canvas is an artboard, this canvas provides a lot of API, we can call the canvas API to draw and do some work on the canvas, Cliprect method to cut a rectangular area on the canvas, The rectangular area is described with a Rect object. After Cliprect is called, the drawing area of the canvas is reduced to the same size as the rectangular area specified by Rect. All draws are limited to the rectangle's range. The cutting concept here is similar to that of the PS cut.

Typical example, drawer layout, looking for NetEase Yun Music surgery:

Note that when the left drawer is open, the drawer layout and the back layout overlap, when the entire screen more than half become red, excessive rendering serious.

When the drawer layout pops up, the drawer layout is opaque, that is, the layout of the content behind the drawer layout is not required to draw, and NetEase cloud was drawn, resulting in the drawer layout area of the pixel points drawn many times.

Google official under the Android.support.v4.widget package has Drawerlayout.java class. Used to implement the layout of the drawer. The class is overriding the Drawchild method:

@OverrideProtectedBooleanDrawchild (canvas canvas, View child,Long Drawingtime) {Finalint height = getheight ();Determine if it is a content viewFinalBoolean drawingcontent = Iscontentview (child);int clipleft =0, clipright = getwidth ();Record Current canvas informationFinalint restorecount = Canvas.save ();if (drawingcontent) {Crop only when the content view is drawnFinalint childCount = Getchildcount ();for (int i =0; i < ChildCount; i++) {Final View v = getchildat (i);if (v = = Child | | v.getvisibility ()! = VISIBLE | |!hasopaquebackground (v) | |!isdrawerview (v) | | v.getheight () < height ) {If child is content view/view not visible/view background transparent/Not drawer view/child height less than parent layout heightDo not make the canvas cutContinue }if (checkdrawerviewabsolutegravity (V, gravity.left)) {//box trimmed left and right final int Vright = V.getright (); if (Vright > Clipleft) clipleft = vright;} else {//box trimmed left and right int vleft = V.getleft (); if (Vleft < clipright) clipright = Vleft;} } //crop canvas canvas.cliprect (clipleft, 0, Clipright, GetHeight ());} //draw child View final boolean result = Span class= "Hljs-keyword" >super.drawchild (canvas, Child, drawingtime); //revert to the canvas before cropping canvas.restoretocount (Restorecount);}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

The Drawchild method is called within the Dispatchdraw method of the ViewGroup class to draw the child view, and the Drawerlayout class overrides the method because the Drawchild method is called before all child views are drawn. However, only the content area view is trimmed, and when the content area view is drawn, the position information of the drawer view is obtained, if the drawer view is visible, the background is opaque, the height of the drawer is consistent with the parent layout, the position of the left, top, right, and bottom edges of the drawer view in the canvas is obtained. It then cuts out parts of the content view that are not blocked, and draws the trimmed canvas to the child view, so that the content area is only drawn in the area after the cut, and the other areas are not drawn. After the child view is drawn, restore the canvas to the state before cropping, because all view under a window uses the same canvas, so you need to restore the status to the other child view.

Here's a "download" APP in the system, using the Drawerlayout implementation:

Although the content area is red in the app, the drawer view is over-drawn when it is out of sight, but less than the portion of the content area that has not been blocked.

3. ImageView background and imagedrawable overlap
In Android, all view settings can be background. ImageView can also set imagedrawable in addition to the ability to set background.

In the development, many times need to display pictures, before the picture is loaded, usually need to display a default picture, many times will use the ImageView Background property to set the default background map, and imagedrawable to set the image to be loaded. This causes a problem, when the image is loaded onto the page, the default background is blocked, but it still needs to be drawn, resulting in an over-rendering situation.

The solution is to set the background map and the pictures that are actually loaded through the Imagedrawable method.

Summarize
    • A window in Android corresponds to all Views under one Canvas,window (View/viewgroup) using the same canvas, The parent node of the view tree will crop the canvas before calling the view.draw of the child view, and the cropped area is the rectangular area that the view occupies on the screen, which is why the content beyond the view boundary is trimmed off.
    • Since the over-drawn value is drawn multiple times at a pixel point, we just need to make sure that the picture or background color doesn't stack together. The correct way to do this is to minimize the overlapping area of the view with the background. If overlapping, use the canvas's cliprect for trimming.
    • Minimize the depth of the view to reduce the traversal of the view tree.

"Go" Android performance optimizations-over-drawing solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.