Principle and optimization of layout rendering process

CPU/GPU

As the "central processing unit", the CPU is not only responsible for logical calculations, but also needs to do memory management and display operations. Therefore, with the emergence of various complex apps, its actual transportation performance will be greatly reduced.
Design reason: In order to improve the graphics display path and complex graphics, the GPU was designed.
Main function: To help the CPU share the graphics display

XML layout display to screen flow

<TextView width="">
|
LayoutInflater加载进内存
|
CPU计算,处理成位图
|
CPU将图形交给GPU
|
GPU对图形进行删格化
|
显示器显示

FPS

12fps: when the number of frames is higher than about 10-12 frames per second, the eyes will think it is coherent;
24fps: sound movie shooting is generally 24 frames per second
30fps: early dynamic video games, generally around 30 frames per second ;
60fps: In the process of mobile phone interaction, touch and feedback are required, and 60 frames are needed to achieve the effect of no freezing.

So if you don't draw a frame in 16ms, you will feel stuck.

Layout loading process

setContentView
|
LayoutInflater(inflate)
|
XmlResourceParser(getLayout)
|
infalte
|
ceateViewFromTag
|
tryCreateView
|
createView
|
View

How to optimize

  • CPU reduces the time to convert xml to object
  • GPU reduces repeated drawing

Overdraw concept

The GPU draws once every 16ms. If the graphics passed by the CPU repeats the position, the user can only see the top screen, while the bottom screen is obscured. Although the bottom part of the drawing is not visible to the user, it also takes up Computing resources have caused unnecessary waste; in this case, the corners are over drawn;

Overdraw viewing tool

"Debug GPU overdrawing" in the developer options

Overdraw optimization

Reduce layout levels

  • Whether the background in the layout is needed;
  • Whether it is possible to delete redundant layouts;
  • Whether the custom View has been cropped accordingly;
  • Whether the layout is flat enough;
  • Use the Merge tag to exclude an extra layer of ViewGroup containers
  • Lazy loading of layout using ViewStub

Layout loading optimization method

Load with java code

  • XML is time-consuming to load due to IO operations and reflection operations
  • Use Java to structure the layout
  • Large amount of code, not realizable, not easy to maintain

X2C library can convert xml layout into Java.
There is also AsyncLayoutInflater provided in Androidx, which can load the layout
asynchronously, and use AsyncLayoutInflater to load the layout asynchronously.

Guess you like

Origin blog.csdn.net/yanwenyuan0304/article/details/106299301