[Java]View PlainCopy
- <span style="FONT-SIZE:18PX;" > </span>
[Java]View PlainCopy
- <span style="FONT-SIZE:18PX;" ></span>
Before drawing, you need to adjust the brush first, until the brush is fine, then draw the image to the canvas so that it can be displayed on the phone screen! The brushes in Android are the paint class, and the paint contains many ways to set its properties, the main common method:
Setantialias: Sets the brush's jagged effect.
SetColor: Setting Brush colors
Setargb: Sets the a,r,p,g value of the brush.
Setalpha: Setting alpha value
Settextsize: Sets the font size.
SetStyle: Sets the brush style, hollow or solid.
Setstrokewidth: Sets the hollow border width.
GetColor: Get the color of the brush
Getalpha: Gets the alpha value of the brush.
Custom controls often encounter the Ondraw () method of overriding view, and the Ondraw () method is often designed to both save () and restore () methods
, and now combine with demo to analyze the effect of these two methods briefly:
1.save () canvas save () method The code after span style= "Font-family:simsun" lang= "ZH-CN", can call canvas< Span style= "Font-family:simsun" lang= "ZH-CN" > translation, retraction, rotation, cropping, and so on!
2.restore (): Used to restore the state that was saved before the canvas, and prevent the operation of the Save () method code from being performed on the canvas, and continue to have an impact on subsequent drawing. This method can avoid the influence of the joint!
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!
Code:
[Java]View PlainCopy
- <span style="FONT-SIZE:18PX;" > Packagecom.test.ui;
- Import Android. R.color;
- Import Android.content.Context;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import Android.view.View;
- Public class Gameview extends View {
- public final static String TAG = "Example_05_03_gameview";
- //Declare Paint object
- private Paint mpaint = null;
- Public Gameview (context context) {
- super (context);
- Mpaint = new Paint ();
- }
- @Override
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- Paint background=new paint ();
- Paint line=new paint ();
- Background.setcolor (Color.darker_gray);
- Line.setcolor (color.red);
- int px = Getmeasuredwidth ();
- int py = Getmeasuredwidth ();
- //Draw background
- Canvas.drawrect (0, 0, px, py, background);
- Canvas.save ();
- Canvas.rotate (px/2, py/2);
- //Draw an upward arrow
- Canvas.drawline (px/ 2, 0, 0, py/ 2, line); //Slash to the left
- Canvas.drawline (px/ 2, 0, px, py/ 2, line); Slash on the right
- Canvas.drawline (px/ 2, 0, px/ 2, py, line); Vertical bar
- Canvas.restore ();
- //Draw Circle
- Canvas.drawcircle (PX- ten, py- ten, line );
- }
- }
- </span>
Mainactivity.java:
[Java]View PlainCopy
- <span style="FONT-SIZE:18PX;" > Packagecom.test.ui;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Public class Mainactivity extends Activity {
- private Gameview Mgameview;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Mgameview = New Gameview (this);
- Setcontentview (Mgameview);
- }
- }
- </span>
Operation Result:
If Canvas.save () and Canvas.restore () are commented out, the circle will run to the left with the previous 90 rotation, not the effect you expect to appear in the lower-right corner:
We clearly see the obvious difference in the position of the circle. Do not proceedCanvasOfSaveAndrestore operation words, All images are rotated in the canvas 90° Drawn on the canvas after the. When executed ondrawsaverestore When the execution of the operation is different, you can draw different graphs, save and restore is often the canvas
Android_ Analysis of the Save () and restore () methods of the canvas