Exercise:https://github.com/zhangbz/AndroidUIPorblems
View over drawing
Turn on "Debug GPU over Drawing" in developer options
Judging criteria
Colorless: Not over-drawn, that is, only once
Blue: one-times over-drawn
Green: Twice times over-drawn
Light red: three times times over-drawn
Red: four times times or more over-drawn
Practice
Where "This is Test" four times or more multiples of the drawing, its background three times times, Button twice times, the text button in three times times, the background one times.
Mainactivity optimization
First analyze the layout file, found that nested linearlayout set the same background color as the parent linearlaytout, so delete its background color settings, the effect is as follows
Then, due to some themes from Android, window will be added by default to a solid color background, because the activity interface has a background full of the screen, so we can get rid of the activity's default drawing, in Mainactivity's OnCreate () method to modify the code.
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); GetWindow (). setbackgrounddrawable (null);//new Code //... }
The effect is as follows:
At this point, the page is eliminated by over three times times and over-drawn.
Overdrawview optimization
The original implementation method is as follows, due to the overlay of the graphics drawn in the OnDraw () method, resulting in over-rendering
@Override protected void OnDraw (canvas canvas) { super.ondraw (canvas); int width = getwidth (); int height = getheight (); Mpaint.setcolor (Color.gray); Canvas.drawrect (0, 0, width, height, mpaint); Mpaint.setcolor (Color.cyan); Canvas.drawrect (0, HEIGHT/4, width, height, mpaint); Mpaint.setcolor (Color.dkgray); Canvas.drawrect (0, HEIGHT/3, width, height, mpaint); Mpaint.setcolor (Color.ltgray); Canvas.drawrect (0, HEIGHT/2, width, height, mpaint); }
Modify the code to avoid overwriting each other
@Override protected void OnDraw (canvas canvas) { super.ondraw (canvas); int width = getwidth (); int height = getheight (); Mpaint.setcolor (Color.gray); Canvas.drawrect (0, 0, width, HEIGHT/4, mpaint); Mpaint.setcolor (Color.cyan); Canvas.drawrect (0, HEIGHT/4, width, HEIGHT/3, mpaint); Mpaint.setcolor (Color.dkgray); Canvas.drawrect (0, HEIGHT/3, width, HEIGHT/2, mpaint); Mpaint.setcolor (Color.ltgray); Canvas.drawrect (0, HEIGHT/2, width, height, mpaint); }
Optimization effect
Busyondraw optimization
There is a noticeable lag when clicking the button, which can be judged from the following code, which should be caused by the time-consuming operation in the OnDraw () method
@Override protected void OnDraw (canvas canvas) { super.ondraw (canvas); for (int i = 0; i < i++) { System.out.println ("canvas = [" + Canvas + "]" + i); } Paint paint = new paint (); Paint.setcolor (color.red); Paint.setstyle (Paint.Style.STROKE); Paint.setstrokewidth (4); int radius = math.min (GetWidth (), getheight ())/2; Canvas.drawcircle (GetWidth ()/2, GetHeight ()/2, radius, paint); }
Optimization scenario: Move time-consuming operations into child threads
New Thread (New Runnable () { @Override public void Run () {for (int i = 0; i <; i++) { SYSTEM.O UT.PRINTLN ("canvas = [" + Canvas + "]" + i);}} ). Start ();
Android Performance Tuning Exercise: Over-drawing