<> lag ? you Android UI Did you learn optimization well ?

<> Why? ? Your development app The program and animation are often stuck in ?

<> first , Let's first understand UI Optimized resolution :

stay Android During application development , The layout code of the control on the screen and the logic code of the program are usually separated . The layout code of the interface is placed in an independent xml In the file , This file is a tree organization , Controls the layout of the page . usually , Many controls will be used in this page , Control uses a lot of resources .Android The system itself has many resources , Include various strings , picture , animation , Styles and layouts, etc , These can be used directly in the application . There are many benefits , Can reduce memory usage , It can also reduce part of the workload , You can also reduce the size of the program installation package .

<> summary :

Actually, after we developed it app, During use , We often see the phenomenon that program animation gets stuck ; Android Applied Caton , Frame loss, etc , Most of these factors that affect the user experience are related to 16ms
This value is related to . Let's talk about it UI Factors affecting application fluency in rendering .

<>16ms definition :

Android System every 16ms Will issue VSYNC Signal redraws our interface (Activity). Why 16ms,
because Android The set refresh rate is 60FPS(Frame Per Second), That is, every second 60 Refresh rate of frames , approximately amount to 16ms Refresh once .

UI Principle of Caton :Android each 16ms Will be drawn once Activity, From the above conclusion, we know that , If for some reason our logic ,CPU time consuming ,GPU Time consuming greater than 16ms,UI You can't finish painting once , Then it will cause Caton .

<> Caton causes and Solutions :

one , Overprint

1. Remove unnecessary background color :

* Set window background color to general background color , Remove root layout background color
* If the page background color is inconsistent with the general background color , Remove window background color after page rendering
* Remove those with the same background color as the list Item Background color
2. Flatten the layout view tree :

* Remove nested layout
* use merge,include label
* Use less performance intensive layouts (ConstraintLayout)
3. Reduce transparency , Namely alpha Use of attributes :

* By using translucent color values (#77000000) replace
two ,UI Complex operation of threads

UI The complex operation of threads will cause UI No response , Of course, more is caused by UI Response stagnation , lag . produce ANR It's the acme of Caton

Solution : Analysis of Caton caused by operation blocking , have access to Traceview This tool .

three , Frequent GC

frequently GC Reason for :

memory thrashing (Memory Churn), That is, a large number of objects are created and immediately released in a short time .

A large number of objects will be generated instantaneously, which will be seriously occupied Young Generation Memory area of , When the threshold is reached , When there is not enough space left , Also triggers
GC. Even if the objects allocated each time require very little memory , But it adds up Heap Pressure of , Which triggers more GC.

Solution : A large number of objects are generated instantaneously because we are in the loop of code new object , Or at onDraw Create objects, etc , Try not to use a lot of local variables in the loop .

Overprint detection :

* stay Android Mobile phone " System settings "–>“ Developer options ”–>“ debugging GPU Overprint ”–>" display GPU Overprint " Medium startup commissioning .
* blue , Pale green , Pale red , Crimson represents 4 Different degrees of over drawing (Overdraw) situation .
* blue : signify overdraw 1 times , Pixels are drawn twice . Big blue is acceptable ( If the entire window is blue , Can get rid of one layer ).
* green : signify overdraw 2 times , Pixels are drawn three times . Medium sized green areas are acceptable , You should try to optimize , Reduce them .
* Pale red : signify overdraw 3 times , Pixels are drawn four times , Acceptable in a small range .
* Crimson : signify overdraw 4 times , Pixels painted five times or more . This is unacceptable , To repair them .
* Our goal is to minimize red
Overdraw, Ideally blue , A pixel is drawn only once , Qualified page drawing is white , Mainly blue , The area above green shall not exceed one third of the whole area , The lighter the color, the better .

IV. time consuming operation

use AsyncTask,Thread,HandlerThread,HandlerThread,IntentService And other means to remove time-consuming operations UI thread .

be careful : If you start the thread yourself , You should call Process.setThreadPriority()
Set the thread's priority by "background". If not , The thread you started may still slow down your app, Because by default it is the same as UI Threads have the same priority .

Optimization ideas :

* Saving references with member variables
* Reuse using object pools
V. too many threads

obviously , This is for app Has a negative impact on the performance of . More threads ,CPU Our resources are limited ,CPU The number of threads that can run at the same time is small ,
All other threads can only wait , meanwhile , Each thread needs to occupy at least 64K Memory for . Too many threads will only cause damage to memory and CPU Fierce competition for resources .

Optimization ideas : Establish unified management of line process pool

Sum up the above : This is a common phenomenon , And Solutions . Of course, there are scrollable lists layout And so on . More technologies are available for free Android Technology, interview questions, core notes .