How to Use Save Restore translate rotate on Android canvas

Source: Internet
Author: User

I. First, let's talk about the SAVE and restore functions of canvas. This is a very attractive part of canvas.

The ondraw method will pass in a canvas object, which is the canvas you use to draw the visual interface of the control.

In the ondraw method, we often see that the SAVE and restore methods are called. What are they used?

Saved save: used to save the canvas status. After saving, you can call operations such as pan, zoom, rotate, miscut, and crop of the canvas.

Restore restore: used to restore the State saved before the canvas. This prevents operations performed on the canvas after the save operation from affecting subsequent painting.

  Save and restore must be paired (Restore can be less than save, but not more)If the number of restore calls is more than that of SAVE, an error is thrown. The SAVE and restore operations on canvas are often mixed.

For example, we first want to draw a right arrow in the canvas. Of course, we can draw it directly. In addition, we can also rotate the canvas 90 ° and draw an up arrow first, and then rotate back (this rotation operation is very useful for marking on the circumference ). Then, if we want to have a 20-pixel circle in the lower right corner, the core code in ondraw is:

Int PX = getmeasuredwidth ();

Int py = getmeasuredwidth ();

// Draw background

Canvas. drawrect (0, 0, PX, Py, backgroundpaint );

Canvas. Save ();

// Take the canvas center as the axis and rotate it 90 degrees forward

Canvas. Rotate (90, PX/2, Py/2 );

// Note the following,Rotate must be followed by getmeasuredwidth () and getmeasuredheight ()... Why is it not clear to me ..

// Draw up arrow, left-right-middle

Canvas. drawline (PX/2, 0, 0, Py/2, linepaint );

Canvas. drawline (PX/2, 0, PX, Py/2, linepaint );

Canvas. drawline (PX/2, 0, PX/2, Py, linepaint );

// Rotate the canvas back to the image before Rotation

Canvas. Restore ();

// Draw circle

Canvas. drawcircle (PX-10, Py-10, 10, linepaint );

 

 

Result 1:

 

Figure 1

 

What will it look like if we don't call save and restore? 2:

 

 

Figure 2

 

From the two graphs, we can see the obvious difference in the circle position. Without the SAVE and restore operations on the canvas, all images are drawn on the canvas after 90 ° rotation.When the execution is completeThe ondraw method, the system automatically restores the canvas back.When the SAVE and restore operations are executed differently, the drawing may be different.

 

Therefore, the special operations on the canvas are often mixed between SAVE and restore.

 

Ii. Next, let's talk about the translate method.

Like the usage of j2's translate, the function is to move the origin point. The default origin point () is in the upper left corner of the screen. You canUse translate (x, y) to take the vertex (x, y) as the origin.

From: http://blog.csdn.net/infsafe/article/details/5428696

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.