We can think of this canvas as a piece of memory that the system provides to us (but actually it's just a drawing API, real memory is the bitmap), and it provides a whole set of methods for manipulating this memory area, all of which are drawing APIs. In this way we have been able to draw a stroke or use graphic to paint what we need, and to draw what we want to show is controlled by ourselves.
This approach can be divided into two types of environments: one is canvas drawing with normal view, and the other is drawing using a dedicated Surfaceview canvas. The main difference between the two is that you can define a dedicated thread in Surfaceview to do the drawing work, and the application does not need to wait for the view brush graph to improve performance. The previous one is suitable for small, low frame rate animation, such as chess games, and the latter is mainly used in the game, high-quality animation aspects of the drawing.
Paint represents the brushes, brushes, pigments, etc. on canvas;
Common methods of paint class:
Setargb (int A, int r, int g, int b)//Set Paint object color, parameter one is Alpha transparent value
Setalpha (int a)//set alpha opacity, range 0~255
Setantialias (Boolean AA)//whether anti-aliasing
SetColor (int color)//Set color, here the Android internal definition has a color class containing some common color definitions
Settextscalex (float ScaleX)//Set text magnification, 1.0f to original
Settextsize (float textSize)//Set Font size
Setunderlinetext (Booleanunderlinetext)//Set Underline
1. The canvas background of the entire control will be filled with color ARBG
Mcanvas.drawargb (122, 10, 159, 163);
2. The canvas background of the entire control will be filled with color ARBG
Mcanvas.drawcolor (Color.Blue);
3, draw the color, but to develop a mode
Mcanvas.drawcolor (Color.Blue, Mode.screen);
4, painting background, with 2 equivalent
Mcanvas.drawpaint (Mpaint);
5, draw a point
Mcanvas.drawpoint (at Mpaint);
6, draw many points here float[] means {x0,y0,x1,y1,x2,y2,x3,y3 ...}
Mcanvas.drawpoints (New float[]{10,11,10,12,10,13,10,14,10,15,10,16}, Mpaint);
7. Draw the Line
Mcanvas.drawline (...);
8, Draw rectangle Rect and RECTF difference?
The precision is different, rect is using the int type as the numeric value, RECTF is using the float type as the numeric value
Rect r = new rect (10,10,50,50);
Mcanvas.drawrect (R, Mpaint);
RECTF RF = new RECTF (10,10,50,50);
Mcanvas.drawrect (RF, mpaint);
Mcanvas.drawrect (Ten, ten, +, mpaint);
9, the ellipse initialization RECTF parameter is (left,top,right,bottom)
RECTF RF = new RECTF (100,100, 200, 250);
Mcanvas.drawoval (RF, mpaint);
10, Draw the Circle (center x0, center y0, radius, paint)
Mcanvas.drawcircle (+, +, mpaint);
11, Draw Arc RECTF object indicates the inner tangent rectangle (left,top,right,bottom)
RECTF RF = new RECTF (100, 100, 200, 200);
Parameters (Rf,startangle, angle, sweepAngle, paint) sweepAngle Indicates whether arc triangles are displayed angle how many degrees
MCANVAS.DRAWARC (RF, MW, +, True, mpaint);
12. Draw rounded Rectangle RECTF is rectangular (left,top,right,bottom)
RECTF RF = new RECTF (100, 100, 200, 200);
50 indicates the X-direction radius, and 20 for the Y-direction.
Mcanvas.drawroundrect (RF,,, mpaint);
13. Draw any polygon
Path PATH = new Path ();
Path.moveto (100, 100);
Path.lineto (200, 200);
Path.lineto (300, 200);
Mcanvas.drawpath (path, mpaint);
14, through the Path object, you can also draw other graphics
Path PATH = new Path ();
Path.addcircle (+, +, Path.Direction.CCW);
Mcanvas.drawpath (path, mpaint);
/*drawbitmap
DrawText
drawpicture*/
/*rect r = new Rect (100,100,200,200);
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Bitmap Bitmap = Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.bg);
Bitmap.compress (Bitmap.CompressFormat.JPEG, N, out);
InputStream in = new Bytearrayinputstream (Out.tobytearray ());
*/
/*picture picture = Picture.createfromstream (Mcontext.getresources (). Openrawresource (r.raw.bg));
Mcanvas.drawpicture (picture); */
15. Draw Bitmap Object
Mcanvas.drawbitmap (Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.bg), mPaint);
16, The matrix contains the processing operation of the bitmap
/*matrix m = new Matrix ();
M.postscale (2, 2);
M.postrotate (60);
Mcanvas.drawbitmap (Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.bg), M, mPaint); * *
17, draw with the matrix parameters of the bitmap, after the Matrix object can be bitmap do related processing, such as rotation, scaling, mobile and other "about the use of the Matrix to separate summary"
/*bitmap Bitmap = Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.bg);
Matrix M = new Matrix ();
M.postscale (2, 2);
M.postrotate (60);
M.posttranslate (300, 300);
Mcanvas.drawbitmap (Bitmap, M, mpaint); * *
Mcanvas.drawbitmap (...); No summary
18. Drawing text
Mcanvas.drawtext ("123", ten, Ten, Mpaint);
Mcanvas.drawtext (...); No summary
For drawing the draw wheel, see: http://blog.csdn.net/lmj623565791/article/details/41722441
Common ways to use Android canvas