Android_ Analysis of the Save () and restore () methods of the canvas

Source: Internet
Author: User

[Java]View PlainCopy
    1. <span style="FONT-SIZE:18PX;" > </span>
[Java]View PlainCopy
    1. <span style="FONT-SIZE:18PX;" ></span>

Before drawing, you need to adjust the brush first, until the brush is fine, then draw the image to the canvas so that it can be displayed on the phone screen! The brushes in Android are the paint class, and the paint contains many ways to set its properties, the main common method:

Setantialias: Sets the brush's jagged effect.
SetColor: Setting Brush colors
Setargb: Sets the a,r,p,g value of the brush.
Setalpha: Setting alpha value
Settextsize: Sets the font size.
SetStyle: Sets the brush style, hollow or solid.
Setstrokewidth: Sets the hollow border width.
GetColor: Get the color of the brush
Getalpha: Gets the alpha value of the brush.

Custom controls often encounter the Ondraw () method of overriding view, and the Ondraw () method is often designed to both save () and restore () methods

, and now combine with demo to analyze the effect of these two methods briefly:

1.save () canvas save () method The code after span style= "Font-family:simsun" lang= "ZH-CN", can call canvas< Span style= "Font-family:simsun" lang= "ZH-CN" > translation, retraction, rotation, cropping, and so on!

2.restore (): Used to restore the state that was saved before the canvas, and prevent the operation of the Save () method code from being performed on the canvas, and continue to have an impact on subsequent drawing. This method can avoid the influence of the joint!

Illustrate by an example:

For example: We would like to draw a right triangle arrow on the canvas, of course, we can draw directly, in addition, we can also first rotate the canvas to the four °, draw an upward arrow, and then rotate back (this rotation is very useful for the marking on the circumference of the circle), and finally, we draw a a circle of pixels!

Code:

[Java]View PlainCopy
  1. <span style="FONT-SIZE:18PX;"  > Packagecom.test.ui;
  2. Import Android. R.color;
  3. Import Android.content.Context;
  4. Import Android.graphics.Canvas;
  5. Import Android.graphics.Color;
  6. Import Android.graphics.Paint;
  7. Import Android.view.View;
  8. Public class Gameview extends View {
  9. public final static String TAG = "Example_05_03_gameview";
  10. //Declare Paint object
  11. private Paint mpaint = null;
  12. Public Gameview (context context) {
  13. super (context);
  14. Mpaint = new Paint ();
  15. }
  16. @Override
  17. protected void OnDraw (canvas canvas) {
  18. Super.ondraw (canvas);
  19. Paint background=new paint ();
  20. Paint line=new paint ();
  21. Background.setcolor (Color.darker_gray);
  22. Line.setcolor (color.red);
  23. int px = Getmeasuredwidth ();
  24. int py = Getmeasuredwidth ();
  25. //Draw background
  26. Canvas.drawrect (0, 0, px, py, background);
  27. Canvas.save ();
  28. Canvas.rotate (px/2, py/2);
  29. //Draw an upward arrow
  30. Canvas.drawline (px/ 2, 0, 0, py/ 2, line); //Slash to the left
  31. Canvas.drawline (px/ 2, 0, px, py/ 2, line); Slash on the right
  32. Canvas.drawline (px/ 2, 0, px/ 2, py, line); Vertical bar
  33. Canvas.restore ();
  34. //Draw Circle
  35. Canvas.drawcircle (PX- ten, py- ten, line );
  36. }
  37. }
  38. </span>

Mainactivity.java:

[Java]View PlainCopy
  1. <span style="FONT-SIZE:18PX;"  > Packagecom.test.ui;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Public class Mainactivity extends Activity {
  5. private Gameview Mgameview;
  6. @Override
  7. public void OnCreate (Bundle savedinstancestate) {
  8. super.oncreate (savedinstancestate);
  9. Setcontentview (R.layout.activity_main);
  10. Mgameview = New Gameview (this);
  11. Setcontentview (Mgameview);
  12. }
  13. }
  14. </span>


Operation Result:

If Canvas.save () and Canvas.restore () are commented out, the circle will run to the left with the previous 90 rotation, not the effect you expect to appear in the lower-right corner:

We clearly see the obvious difference in the position of the circle. Do not proceedCanvasOfSaveAndrestore operation words, All images are rotated in the canvas 90° Drawn on the canvas after the. When executed ondrawsaverestore When the execution of the operation is different, you can draw different graphs, save and restore is often the canvas


Android_ Analysis of the Save () and restore () methods of the canvas

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.