I. Impact of page rendering on App performance

Main impact of drawing performance: the page display speed of App.

The essence of rendering affects performance: the measurement & rendering time of a page, the measurement & rendering process of a page completed recursively.

Second, draw optimization thinking and direction

View onDraw() avoids doing a lot of work

Directions: Reduce the complexity of view.ondraw () and avoid overdrawing

Iii. Draw specific optimization schemes

Reduce the complexity of view.ondraw ()

1. Do not create new local objects in onDraw()

Because onDraw() can be called so frequently that a large number of temporary objects are generated at once, it not only takes up too much memory but also causes the system to gc more frequently, reducing the efficiency of the program.

2, onDraw() do not do time-consuming operations

Do not perform thousands of loops. Although each loop is lightweight, a large number of loops can eat into the CPU’s timeslice, which can cause the view to draw poorly and lead to UI stutter (more on this later). The View should be drawn at a frame rate of 60 FPS, i.e., no more than 16ms per frame (16ms = 1000/60).

(2) Avoid Overdraw

1. Remove the default Window background
  • The application inherits a windowBackground theme by default, such as the default Light theme. In general, the default Window background is almost useless because the background is customized. If not removed, all interfaces will be drawn one more time.
<style name="Theme.Light">
    <item name="isLightTheme">true</item>
    <item name="windowBackground">@drawable/screen_background_selector_light</item>
    ...
</style>
Copy the code
  • Solution: Remove the default Window background
Method 1: Add the following line attribute <item name= to the application subject"android:windowBackground">@android:color/transparent</item> <! --> <item name="android:windowBackground">@null</item> Method 2: Remove getWindow().setBackgroundDrawable(null) from BaseActivity onCreate() using the following code; <! - or - > getWindow (). SetBackgroundDrawableResource (android. R.c olor. Transparent);Copy the code
2. Remove unnecessary background from the control
  • Scenario 1: RecyclerView/ListView and Item

RecyclerView/ListView and its child control (item) background is the same white, so you can remove the child control (item) layout background.

  • Scenario 2: ViewPager and Fragment

The home page consists of one ViewPager and multiple fragments. If each Fragment has a background color, the ViewPager can be removed.

3. Reduce the hierarchy of layout files (nesting)
  • Using layout labels<merge/><ViewStub/>,<include/>
  • Choose appropriate layout types (LinearLayout/RelativeLayout)

4. UI fluency optimization

(I) REASONS for UI stagnation

The best way to draw a View is to keep it at 60 frames per second, which requires no more than 16ms (1000/60) per frame. If android can’t render the interface in 16ms, it will stall.

(2) Analysis of the causes of UI stuck

  • 1. Perform slight time-consuming operations in the UI thread, causing the UI thread to stall
  • 2. The Layout is too complex to complete rendering in 16ms
  • 3. Too many animations are executed at the same time, resulting in heavy load on the CPU and GPU
  • 4. OverDraw, which causes pixels to be drawn multiple times within the same frame, and overloads CPU and GPU
  • 5. View frequently triggers measure and layout, resulting in too much accumulated time of measure and layout and frequent re-rendering of the whole View
  • 6. Frequently triggering GC operation will cause thread pause, which will make Android system unable to complete drawing within 16ms
  • 7. Redundant resources and logic lead to slow loading and execution
  • 8 ANR.

(2) UI stuck optimization

1. Layout optimization
  • Use include, ViewStub, merge
  • Avoid overly nested and redundant layouts
  • Use custom views instead of complex views
ListView optimization
  • Reuse convertView
  • Sliding not loading
3. Background and image optimization
  • The thumbnail
  • Image compression
4. Avoid ANR
  • Do not do time-consuming operations in the UI thread