Android Performance Optimization series--understanding Overdraw

Source: Internet
Author: User

Over-plotting (overdraw) means that pixels are drawn several times within a frame of time;
Theoretically, a pixel is optimal to draw once at a time, but because the stacked layout causes some pixels to be drawn more than once, each drawing corresponds to a set of drawing commands and GPU operations for the CPU, so repeated plotting of overlapping invisible elements results in additional calculations. It is necessary to minimize the occurrence of overdraw.

The Android system provides the option to measure Overdraw, in the developer Options-debug GPU over-draw (Show GPU Overdraw), open the option to see the status of the current page overdraw.

Depending on the number of overdraw, different colors will be displayed to differentiate

    • transparent = no overdraw
    • Blue = 1 Layer
    • Green = 2 Layers
    • light-red = 3 Layers
    • Dark red = You ' re doing it wrong
Optimization method

The general principle is: try to avoid overlapping the drawing of invisible elements

    1. Remove unwanted background resources

      • added in theme android:windowbackground="null" ;
      • Set GetWindow () in activity. Setbackgrounddrawable (NULL)
        This method should be after Setcontentview (), because GetWindow (). SetBackground (drawable) will talk about drawable settings here Decorview background, The default is 0xff000000, and Setcontentview will initialize the Phonewindow Decorview for the first time;
    2. Segment setting background
      Sometimes in order to facilitate the layout to set a whole background, and then set the background for sub-view, here will also cause overlap, if the Sub-view width mach_parent, you can see completely covered the layout of the part, here you can set the background to reduce repainting.

    3. View OnDraw () method
      Customize view drawing to avoid overlapping portions of the drawing, you can use

canvas// 裁剪canvascanvas// 判断矩形区域是否相交
Other drawing optimization recommendations
    1. Avoid allocating memory and creating objects in the OnDraw function, which will cause frequent garbage collection to degrade performance;
      Doing these things in the initialization, or the animation gap
    2. Reduction of invalidate calls
    3. Keep layout as flat as possible, call Requestlayout () as little as possible, requestlayout will cause the system to traverse the entire view tree to measure and layout, and if the layout is nested complex, there will be performance issues
    4. If the layout is complex, you can consider customizing the ViewGroup for special handling
Reference

Android Performance Patterns:understanding Overdraw
Android performance Patterns:invalidations, Layouts, and performance

Android Performance Optimization series--understanding Overdraw

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.