Understanding of Android Canvas.save () and Canvas.restore ()

Source: Internet
Author: User

A buddy in the group was asked what the role of Canvas.restore () was, and what it was often asked was what the role of Canvas.save () was. The recent development of a shipping project in Shanghai, a sneak summary of the use of two methods, will also be in the blog in the future to record some of the interview may be asked some other questions.

Canvas is the equivalent of a canvas, and the following methods are commonly used in this class:

    • DrawRect (RECTF rect, paint paint)
    • DrawPath (path path, paint paint)
    • Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint)
    • DrawLine (float StartX, float starty, float stopx, float stopy,
      Paintpaint)
    • Drawpoint (float x, float y, paint paint)
    • DrawText (String text, float x, floaty, paint paint)
    • Drawcircle (float CX, float CY, float radius,paint Paint)
    • DrawArc (RECTF oval, float startangle, float sweepAngle, Boolean usecenter, Paint paint)

What's the use of Canvas.save () and Canvas.restore ()?
-Canvas.save (): Used to save the state of the canvas
-Canvas.restore (): Used to restore the state after the canvas is rotated, scaled, and so on, and when used with Canvas.save (), reverts to the state of the Canvas.save () when it is saved.

Let's write a simple demo to verify this conclusion.

1. Get the wide and high dimensions of the view

int x = getMeasuredWidth();int y = getMeasuredWidth();

2. Draw the outermost circle

//画最外面的圆canvas222, paint);


3. Draw a scale similar to the dial effect

//Draw scale  Paint.setcolor (Color.Blue); canvas . DrawLine (x/2 , Y/ 8 , X/2 , 0 , Paint); canvas . Save (); //Note that the method of saving the canvas is called here  for  (int  i = 0 ; i < 3 ; i++) {canvas .  (30 , X/2 , Y/    2 ); canvas . DrawLine (x/2 , Y/ 8 , X/2 , 0 , paint);}  


At this point, the canvas has been rotated clockwise by 3 30 degrees, or 90 degrees. Also, the Canvas.save () method was called before the canvas was rotated, so the state of the canvas that was saved at this time is normal when it is not rotated.

4, continue to draw a line, observe the effect

//给画笔加粗,换成黄色paint.setStrokeWidth(10);paint.setColor(Color.YELLOW);//画一条黄色粗线,便于区分canvas.drawLine(x2y2x2y4, paint);


Observing the operation, the newly drawn yellow line has been rotated 90 degrees. Why is it? Because the canvas has been rotated 90 degrees before.

The following is a call to Canvas.restore () to restore the state of the canvas before drawing the Yellow Line code

canvas.restore();//恢复画布状态paint.setStrokeWidth(10);paint.setColor(Color.YELLOW);canvas.drawLine(x2y2x2y4, paint);

Run effect at this time

The Yellow line resumed its normal display.
Of course, instead of using Canvas.restore (), you continue to rotate-90-degree canvas, and you can restore the canvas to its original state.

canvas.rotate(-9022);

Interested readers can put the call of Canvas.save () behind the rotated canvas 90-degree code, and run the program again to see what's changed.

Source code Download

Understanding of Android Canvas.save () and Canvas.restore ()

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.