Android Canvas Save (), Savelayer () and restore () talking about

Source: Internet
Author: User

Save () Savelayer () restore ()

1. in custom controls you should draw the Onmeasure and onlayout when you are done, and sometimes you need to add some adornments to your control to meet your needs .

Carbon OnDraw (canvas canvas), where canvas is like a canvas, and the style of your custom control is done on top of it .

Canvas, paint and other basic concepts will not be discussed.

2. The following is a direct use of demo to explain the title listed methods first introduce save () and

Knowledge you must know: http://www.cnblogs.com/liangstudyhome/p/4126002.html

Save () : used to save the state of the canvas , thecode after the Save () method , you can call canvas panning, scaling, rotation, cropping and other operations!

Restore (): Used to restore the state of the Canvas before it is saved ( can be thought of as saving the state of the Axis ), to prevent the Save () method code after the The actions that the Canvas performs continue to have an impact on subsequent drawing, which can be avoided by this method

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!

The solution to the problem on the internet said to be rotated back, I feel in fact save () is the state of the coordinates of the canvas.

MyView:

1  Public classMyViewextendsView {2 3      Public Final StaticString TAG = "Example";4 5     PrivatePaint Mpaint =NULL;6 7      PublicMyView (Context context) {8         Super(context);9Mpaint =NewPaint ();Ten     } One  A      PublicMyView (Context context, AttributeSet attrs) { -         Super(context, attrs); -     } the  -      PublicMyView (context context, AttributeSet attrs,intDefstyle) { -         Super(context, attrs, defstyle); -     } +  - @Override +     protected voidOnDraw (canvas canvas) { A         Super. OnDraw (canvas); atPaint background =NewPaint (); -Paint line =NewPaint (); -Line.setstrokewidth (4); - Background.setcolor (color.gray); - Line.setcolor (color.red); -  in         intPX = 500; -         intPY = 500; to  +Canvas.drawrect (0, 0, px, py, background); - Canvas.save (); theCanvas.rotate (PX/2, PY/2); *         //draw an upward arrow $Canvas.drawline (PX/2, 0, 0, PY/2, line);//the left SlashPanax NotoginsengCanvas.drawline (PX/2, 0, px, PY/2, line);//Slash on the right -Canvas.drawline (PX/2, 0, PX/2, py, line);//Vertical Bar the  + Canvas.restore (); ACanvas.drawcircle (px-100, py-100, 50, line); the  +     } -  $}

The effect after operation is:

commenting out the two lines of code Canvas.save () and Canvas.restore () will run with the following effects:

Why does this difference occur?

before Canvas.save (), the canvas's axes are:

Save () then saves the axis state of this state,

Canvas.rotate (PX/2, PY/2) rotates around the center, the axes become:


1 // the left Slash 2 canvas.drawline (PX/2, 0, px, PY/2, line); // Slash on the right 3 canvas.drawline (PX/2, 0, PX/2, py, line); // Vertical Bar

Some of these columns are drawn on the transformed axis, so it's a right-pointing arrow.

when calling Canvas.restore () after the axis reverts to the state before Canvas.save (). So the Canvas.drawcircle (px-100, py-100,line) reference axes are the axes before Cnavas.save () .

This also makes it possible to say why the Canvas.save () and the Canvas.save () circle position do not have the same reason.


Savelayer

Canvas can be seen as a canvas in general, with all the drawing operations such as Drawbitmap and drawcircle on this canvas, which also defines properties such as matrix, color, and so on. However, if you need to implement some relatively complex drawing operations, such as multi-layer animation, maps (maps can have multiple layers of map overlay, such as: Political district, road layer, point of interest layer). Canvas provides layer support, which, by default, can be thought of as having only one layer of layers. If you need to draw at a level, Android canvas can use Savelayerxxx, Restore to create some middle layers, which are managed according to the "Stack structure":


Create a new layer to "stack", you can use Savelayer, Savalayeralpha, from the "stack" to roll out a layer, you can use Restore,restoretocount. However, when the layer is in the stack, subsequent drawxxx operations occur on the layer, and when the layer is retired, it will "draw" the image drawn at the level to the top or canvas, and when you copy the layer to the canvas, you can specify the layer's transparency (layer) , which is specified when creating a layer: public int savelayeralpha (RECTF bounds, int alpha, int saveflags) in this case layers Describes the basic use of layers: canvas can be seen as a two layers (layer), in order to better illustrate the problem, we will change the code slightly, the default layer to draw a red circle, the new layer to draw a blue circle, the new layer's transparency is 0x88.

1  Public classLayersextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6Setcontentview (NewSampleview ( This));7     }8 9     Private Static classSampleviewextendsView {Ten         Private Static Final intLayer_flags = Canvas.matrix_save_flag |Canvas.clip_save_flag One| Canvas.has_alpha_layer_save_flag |Canvas.full_color_layer_save_flag A|Canvas.clip_to_layer_save_flag; -  -         PrivatePaint Mpaint; the  -          PublicSampleview (Context context) { -             Super(context); -Setfocusable (true); +  -Mpaint =NewPaint (); +Mpaint.setantialias (true); A         } at  - @Override -         protected voidOnDraw (canvas canvas) { - Canvas.drawcolor (Color.White);  -Canvas.translate (10, 10);  - Mpaint.setcolor (color.red);  inCanvas.drawcircle (75, 75, 75, Mpaint);  -Canvas.savelayeralpha (0, 0, $, 0x88, Layer_flags);  to Mpaint.setcolor (Color.Blue);  +Canvas.drawcircle (125, 125, 75, Mpaint);  - Canvas.restore (); the          } *     } $}

Analysis:canvas.savelayeralpha (0, 0, $, $, 0x88, layer_flags) pushes a LAYER into the stack, and subsequent

1 Mpaint.setcolor (color.blue); 2 canvas.drawcircle (mpaint);

Drawing a blue circle is drawn in this layer, not the same layer as the red circle before.

The layer that was saved in Canvas.restore () is on the red circle layer.

Is:

Demo download

Android Canvas Save (), Savelayer () and restore () talking about

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.