Android-canvas.save () Canvas.restore () Summary

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.