Save (), Savelayer () and restore () Resolution for canvas in Android

Source: Internet
Author: User

1. Save () method:
To save the state of the canvas, the code after the Save () method can call the canvas's panning, indenting, rotating, cropping, and so on!
2. Restore () Method:
Used to restore the state that was saved before the canvas (which can be thought of as preserving the state of the axis), preventing the operation of the Save () method code from being performed on the canvas continues to have an impact on subsequent drawing, which can be avoided by this method

Summary: The state that was drawn before save will be saved and will not be changed by the state after the restore method is drawn.

Example Description:
For example: We would like to draw a right triangle arrow on the canvas, of course, we can draw directly, in addition, we can first rotate the canvas 90 °, draw an upward arrow, and then rotate back (this rotation is very useful for drawing the marks on the circumference of the circle), and finally, we draw a 20-pixel circle in the lower right corner!

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.

/** * Test Canvas.save and Canvas.restore method * @Project app_view * @Package Com.android.view.canavssav E * @author chenlin * @version 1.0 * @Date May 7, 2014 * * Public  class canvasview extends View {     Public Final StaticString TAG ="Canvasview";Private intMwidth = -;Private intMheight = -; Public Canvasview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); } Public Canvasview(context context, AttributeSet attrs) { This(Context, Attrs,0); } Public Canvasview(Context context) { This(Context,NULL); }@Override    protected void OnDraw(Canvas canvas) {//Draw background BrushPaint Backpaint =NewPaint (); Backpaint.setcolor (Color.gray);//Draw line brushesPaint Linepaint =NewPaint (); Linepaint.setstrokewidth (4); Linepaint.setcolor (color.red);//Draw backgroundCanvas.drawrect (0,0, Mwidth, Mheight, Backpaint); Canvas.save ();//Rotate canvasCanvas.rotate ( -, Mwidth/2, Mheight/2);//Draw three linesCanvas.drawline (Mwidth/2,0,0, Mheight/2, Linepaint); Canvas.drawline (Mwidth/2,0, Mwidth, Mheight/2, Linepaint); Canvas.drawline (Mwidth/2,0, Mwidth/2, Mheight, Linepaint); Canvas.restore ();//Draw a circle after recovery, remain intact, no rotationCanvas.drawcircle (Mwidth- -, Mheight- -, -, Linepaint);Super. OnDraw (canvas); }@Override    protected void onsizechanged(intWintHintOLDW,intOLDH) {//mwidth = w;        //mheight = h;        Super. onsizechanged (W, H, OLDW, OLDH); }}

1) Pictures after Canvas.save

2) No picture of Save

usage of 3.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":

Example code:

 Public  class layerview extends View {    Private Static Final intRADIUS = the;Private Static Final intLayer_flags = Canvas.matrix_save_flag | Canvas.clip_save_flag | Canvas.has_alpha_layer_save_flag | Canvas.full_color_layer_save_flag | Canvas.clip_to_layer_save_flag;PrivatePaint Mpaint; Public Layerview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); } Public Layerview(context context, AttributeSet attrs) {Super(context, attrs); Mpaint =NewPaint (); Mpaint.setantialias (true); } Public Layerview(Context context) {Super(context); Mpaint =NewPaint (); Mpaint.setantialias (true); }@Override    protected void OnDraw(Canvas canvas)        {Canvas.drawcolor (color.white); Canvas.translate (Ten,Ten);//Draw the first circle, on the Layer1Mpaint.setcolor (color.red); Canvas.drawcircle (RADIUS, radius, radius, mpaint);//Set the transparency of the second layerCanvas.savelayeralpha (0,0, -, -,0x88, layer_flags);//Draw the second circle, on the Layer2Mpaint.setcolor (Color.Blue); Canvas.drawcircle (RADIUS + the, RADIUS + the, RADIUS, Mpaint);//Set the transparency of a third layerCanvas.savelayeralpha ( -, -, -, -,0x88, layer_flags);//Draw the third circle, on the Layer3Mpaint.setcolor (Color.green); Canvas.drawcircle (RADIUS + -, RADIUS + -, RADIUS, Mpaint);    Canvas.restore (); }}

Results

Save (), Savelayer () and restore () Resolution for canvas in Android

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.