A lot of data point out that canvas is actually a canvas, and we use it to give a variety of tools (APIs)
Add a magical paintbrush (paint Class) to draw a variety of shapes on the canvas. There are two key points, one of which is the paint control canvas area. (You must draw on the paper AH) The second is the actual area of the painting control, the computer must be very low IQ, it does not know what is the canvas, it is too advanced, it feels, you just tell where the painting? Such a picture has a coordinate system, the computer is only in the positive x and positive y range of the axis to draw, but the computer can no matter whether the canvas in the area there is no! If it does not have a canvas in its range, it cannot be drawn. Even if he does not see it, the canvas must be within a reasonable range of coordinates so that the canvas can be painted.
Since the computer draws only the coordinates, you give a coordinate system (determine the location of the origin on your screen, possibly outside the screen), and then explicitly tell it where to start drawing.
The purpose of save is to preserve the canvas state, which is actually the position of the origin of the coordinate system and the direction of the x-axis and the y-axis.
See in that example. When you are panning, rotating, stretching, and so on, the operation is actually done for the entire coordinate system. For example, you draw a point in the (30,40) position and then rotate 45 degrees so that the position of (30, 40) is changed because the position of the coordinate system is changed. If you miss the coordinate position before save, just call the Restore () method to remove that coordinate system from the stack. You draw again based on the position of the coordinates before save.
The following example is the validation of the above reasonable explanation, the code has comments, I inherited the TextView class extension of the Mytextview class.
Layout file Mytextview.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <Com.example.draw.MyTextViewAndroid:layout_width= "200DP"Android:layout_height= "200DP"Android:text= "Eat dinner Tonight"/> <Com.example.draw.MyTextViewAndroid:layout_width= "200DP"Android:layout_height= "200DP"Android:text= "Eat dinner Tonight"/></LinearLayout>
OnDraw inside the Code
protected voidOnDraw (canvas canvas) {intHeight = getmeasuredheight ();//get the height of the canvas area intwidth = Getmeasuredwidth ();//get the width of the canvas area intPX = WIDTH/2; intPY = HEIGHT/2; Paint P1=NewPaint (Paint.anti_alias_flag); P1.setcolor (color.red); //give the canvas area (red part)Canvas.drawrect (0,0, WIDTH,HEIGHT,P1); //determine the location of 200,200//because we give the size of the control in the parent control to 200DP,200DP//so the center position of the control is (200,200) both height and width areP1.setcolor (Color.yellow);//Yellow circle.Canvas.drawcircle (200,200,20, p1); P1.setcolor (Color.Black); P1.setstrokewidth (10); P1.setstyle (Paint.Style.FILL_AND_STROKE); //draw the position of the current origin.Canvas.drawcircle (0, 0, 20, p1); Canvas.drawline (0, 0, 0, 1000, p1); Canvas.drawline (0, 0, 1000, 0, p1); Canvas.save ();//Save the state of the above coordinate system and putCanvas.translate (100, 300);//Move the axis right (DX) 100 Down (DY)//draw the Origin position again and draw the x-axis and y-axis .P1.setcolor (Color.green); Canvas.drawcircle (0, 0, 20, p1); Canvas.drawline (0, 0, 0, 1000, p1); Canvas.drawline (0, 0, 1000, 0, p1); Canvas.restore ();//revert to the previous coordinate system//rotate the coordinate system around px,py 30 degreesCanvas.rotate (30, px, py); Canvas.drawpoint (px, py, p1); //draw the position of the origin and draw the x-axis and the y-axis .P1.setcolor (Color.Blue); Canvas.drawcircle (0, 0, 20, p1); Canvas.drawline (0, 0, 0, 1000, p1); Canvas.drawline (0, 0, 1000, 0, p1); Super. OnDraw (canvas);}
Android-canvas's Save and restore