I'm sure a lot of people have read the official document to the canvas Save (), the Restore () method is also a header fog, save () What exactly is the message saved?
The answer is: the origin of the coordinate system, information about the axis direction.
When we use canvas, we will inevitably use the transitoin (), rotate () method to change the orientation of the coordinate system's origin and Axis, and save () saves this information.
Here's a chestnut:
This example uses canvas to draw a dashboard. Customize view to draw the dashboard in the OnDraw () method.
1 /**2 * Created by Administrator on 2015/2/1 0001.3 */4 Public classImagematrixviewextendsView {5 6 7 PublicImagematrixview (Context context) {8 Super(context);9 Ten } One A PublicImagematrixview (Context context, AttributeSet attrs) { - Super(context, attrs); - the } - - + @TargetApi (build.version_codes. LOLLIPOP) - @Override + protected voidOnDraw (canvas canvas) { A Super. OnDraw (canvas); atBrushes are used to draw circles and scales -Paint Paintline =NewPaint (); - Paintline.setcolor (color.blue); - Paintline.setstyle (Paint.Style.STROKE); -Paintline.settextsize (20); -Brushes for drawing pointers inPaint Paintpointer =NewPaint (); -Paintpointer.setstrokewidth (10); toGet the radius of a circle + floatRadius = Math.min (GetWidth ()/2, GetHeight ()/2); - the //Draw a circle * canvas.drawcircle (radius, radius, radius, paintline); $ //change coordinate system, after change, coordinates origin in (radius, radius), x axis Square horizontal right, Y axis positive direction horizontal downwardPanax Notoginseng canvas.translate (radius, radius); - the //Saving coordinate system Information + Canvas.save (); A the //draw a dot on a circle + for(inti = 0; I < 12; i++) - { $ if(i% 3 = = 0) $ { -Canvas.drawline (0,-radius, 0,-(radius-20), paintline); - } the Else - {WuyiCanvas.drawline (0,-radius, 0,-(radius-10), paintline); the } -Canvas.drawtext (String.valueof (i), 0,-(radius-24), paintline); Wu //rotate the coordinate system 30 degrees clockwise, -Canvas.rotate (30, 0, 0); About } $ - //back to last saved state: Coordinate origin in (radius, radius), x-axis positive direction horizontal right, y-axis positive direction horizontal downward - Canvas.restore ();
Continue to change the axis in the saved state -Canvas.rotate (30);
Draw Pointer ACanvas.drawline (0, 0, 40, 0, paintpointer); +Canvas.rotate (30); theCanvas.drawline (0, 0, 60, 0, paintline); the } the}
:
Android-canvas.save () Canvas.restore () Summary