1. Principle of drawing
1.CPU is responsible for calculating the display content (measurement, position, drawing, etc.); 2.GPU is responsible for rasterization and rendering (rendering UI elements onto the screen); 3.16 MS sends VSYNC signal to trigger UI rendering; 4. Screen refresh rate on most Android devices :60Hz;Copy the code
Ii. Optimization tools:
Systrace: 1). Focus on Frames 2). Normal: green dots; Frame loss: yellow or red dots; 3).Alerts column (search for modification direction according to information); 2.Layout Inspector: 1). View the view hierarchy; Choreographer Class 1). Get FPS for online use in real time; A. API16 after use; b.Choreographer.getInstance().postFrameCallBack;Copy the code
Three.Android layout loading principle:
Performance bottlenecks: 1. Layout file parsing :IO process; 2. Create View object: reflection; LayoutInflater.Factory: 1.LayoutInflater creates a View Hook; 2. Customized View creation process: global replacement of customized TextView, etc.; Factory and Factory2:Factory2 inherits from Factory; Additional argument :parent;Copy the code
Four. Elegant access to the interface layout time:
1. Conventional way: Background: obtain the loading time of each interface implementation: copy method, manual buried point; Disadvantages: inelegant, intrusive code; 2. AOP implementation: the tangent point: the Activity of the setContentView @ Around (" execution (* android app. The Activity. The setContentView (..) ARTHook implementation 4. How to obtain the loading time of each control? 1). Low invasive; 2). Use LayoutInflater. Factory;Copy the code
Asynchronous Inflate:
Slow reading of layout files :IO process; Create View slow: reflection (3 times slower than new); 1. Fundamental solution: start with IO and slow reflection to solve; 2. Side relief: it does not affect the main thread and does not take time in the main thread; AsyncLayoutInflater: asynchronous Inflate 1.WorkThread loads the layout; 2. Callback main thread; 3. Save the main thread time; Disadvantages: Can't set LayoutInflater.Factory, not backward compatible, need to customize AsyncLayoutInflater.Copy the code
Six. Layout loading optimization actual combat
IO operation, reflection slow 2.AsynclayoutInflater only ease; Java code writing layout: Essentially solves performance problems and introduces new ones: poor development and maintainability; X2C introduction: Retain the advantages of XML and solve its performance problems 1. 2. Principle :APT compilation translates XML into Java code; Disadvantages: Some attributes are not supported by Java; Lost system compatibility (AppCompat), can modify the framework;Copy the code
Seven. View rendering optimization of actual combat
Drawing process: 1. Measurement: determine the size; 2. Layout: determine the location; 3. Draw: Draw a view; Performance bottlenecks: 1. 2. Top-down traversal; 3. Trigger multiple times; Layout hierarchy and complexity: Criteria: Reduce View number hierarchy; Wide and shallow, avoid narrow and deep; ConstraintLayout: 1. Implement almost completely flat; 2. Higher performance in building complex layouts; 3. With RelativeLayout and LinearLayout; Optimization: 1. Use RelativeLayout without nesting. 2. Do not use weight in a nested LinearLayout; 3. Merger tag: Reduce by one level, can only be used for root View; Overdraw: 1. A pixel should be drawn only once; 2. Debug GPU overdrawing; 3. Blue is acceptable. Methods to avoid over-drawing: 1. Remove redundant background colors and reduce the use of complex shapes; 2. Avoid stacking layers; 3. Custom View is drawn with clipRect shielding and covered View; Other techniques: 1.Viewstub: efficient placeholder, delayed initialization; 2. Avoid: creating large objects in onDraw, time-consuming operation 3Copy the code
Eight. Layout optimization simulation interview
1. What tools are used in layout optimization? Comprehensive use: ChoreoGrapher, AOP, hooks, Systrace, Layout Inspector; 2. Why does the layout stall and how to optimize it? Reason :IO, reflection, traversal, redraw; Solution: Asynchronous synchronized,X2C, reduces layout hierarchy, avoids redrawing, etc. AOP, monitoring; 3. Complete layout optimization output? 1. Systematic monitoring means: offline + online; 2. Metrics :FPS, load time, layout level; 3. Core path guarantee;Copy the code