Android uses canvas to draw various shapes (points, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles)

Source: Internet
Author: User
Tags drawtext

1, first say the Canvas class: class overview The Canvas class holds the "draw" calls. To draw something, your need 4 basic components:a Bitmap to hold the pixels, A Canvas to host the draw calls (writing into The bitmap), a drawing primitive (e.g. Rect, Path, Text, bitmap), and a paint (to describe the colors and styles for the Drawing).

This class is equivalent to a canvas, you can draw a lot of things inside;

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.

The following are common methods of the canvas class:

DrawRect (RECTF rect, paint paint)//plot area, parameter one for RECTF one area

DrawPath (path path, paint paint)//Draw a path, parameter

Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint)//map, parameter one is our regular Bitmap object, parameter two is the source area (here is Bitmap), the parameter three is the target area ( Should be in the canvas location and size), parameter four is the paint brush object, because it is possible to use scaling and stretching, and when the original rect is not equal to the target rect, there will be a significant loss of performance.

DrawLine (float StartX, float starty, float stopx, float stopy, paintpaint)//Draw line, the x-axis position of a starting point of the parameter, the y-axis position of the parameter two starting point, the x-axis horizontal position of the three end of the parameter, Parameter four the vertical position of the y-axis, and the last parameter is the paint brush object.

Drawpoint (float x, float y, paint paint)//Draw point, parameter one horizontal x axis, parameter two vertical y axis, third parameter is paint object.

DrawText (string text, float x, floaty, paint paint)//rendered text, canvas class In addition to the above can also depict text, parameter one is a String type of text, parameter two x axis, parameter three Y axis, Parameter four is the Paint object.

DrawOval (RECTF oval, paint paint)//Draw ellipse, parameter one is scan area, parameter two is Paint object;

Drawcircle (float CX, float CY, float radius,paint paint)//Draw circle, parameter one is the X axis of center point, parameter two is the Y axis of center point, parameter is radius, parameter four is Paint object;

DrawArc (RECTF oval, float startangle, float sweepAngle, Boolean usecenter, Paint paint)//ARC,

Parameter one is the RECTF object, the boundary of a rectangular area ellipse is used to define the shape, size, arc, parameter two is the starting angle (degrees) at the beginning of the arc,

Parameter three sweep angle (degrees) begins to measure clockwise, parameter four is if this is true, including the Oval center arc, and close it if it is false it will be an arc, parameter five is the Paint object;

Also understand a paint class:

class overview The Paint class holds the style and color information about what to draw geometries, text and bitmaps.

The Paint class has style and color information on how to draw geometry, text, and bitmaps.

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

2, see the case directly in this case, we use a custom view class;

Customactivity.java

01.public class Customactivity extends Activity {  02.    @Override   03.    public void OnCreate (Bundle savedinstancestate) {  04.        super.oncreate (savedinstancestate);  05.        setcontentview (R.layout.main);  06.        init ();  07.   }  08.  09.     private void init () {  10.        linearlayout layout= (linearlayout) Findviewbyid (r.id.root);  11.        final Drawview view=new DrawView (This) ;  12.        view.setminimumheight ($);  13.         view.setminimumwidth (+);  14.       // Notification View component Redraw     15.        view.invalidate (); &nbSp 16.        Layout.addview (view);  17.           18.   }  19.}  

Important class Custom view component to override the View component's OnDraw (Canvase) method, the next step is to draw a large number of geometries on the canvas, points, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles, and so on in various shapes! Drawview.java

01.public class Drawview extends View {  02.  03.    Public Drawview (context context) {  04.         Super (context);  05.   }  06.  07.     @Override   08.    protected void OnDraw (canvas canvas) {  09.   & nbsp;    Super.ondraw (Canvas);  10.       /* 11.          * Method Description DrawRect Draw Rectangle Drawcircle Draw Circle DrawOval Draw Ellipse DrawPath draw freeform 12. & nbsp;       * DrawLine draw a line drawpoin draw a point 13.          */  14.       //Create brushes   15.         Paint p = new paint ();  16.        p.setcolor (color.red);//Set Red   17.  18.        canvas.drAwtext ("Draw circle:", ten, p);//Draw Text   19.        canvas.drawcircle (P);// Small Circle   20.        P.setantialias (TRUE);//Set the brush's jagged effect. True is the removal, we see the effect on the   21.        canvas.drawcircle (+, A, p);//Great circle   22.  23.        Canvas.drawtext ("Draw lines and arcs:", ten, p);  24.         P.setcolor (Color.green);//Set Green   25.         Canvas.drawline (max, Max, Max, p);//Draw line   26.        Canvas.drawline ( , p);//Slash   27.       //Draw smiley arc   28.         P.setstyle (Paint.Style.STROKE);//Set hollow   29.         RECTF oval1=new RECTF (150,20,180,40);  30.        CANVAS.DRAWARC(Oval1, p);//small arcs   31.        Oval1.set (190, 20, 220, 40);   32.        Canvas.drawarc (Oval1, p);//small arcs   33. & nbsp;      Oval1.set (; ) 34.         Canvas.drawarc (oval1, 0, N/A, false, p);//Small arc   35.  36.        Canvas.drawtext ("Draw rectangle:", ten, p);  37.        P.setcolor (Color.GRAY);// Set gray   38.        P.setstyle (Paint.Style.FILL);//Set fill   39.         Canvas.drawrect (max., p),//square   40.         Canvas.drawrect (max, N, p),//Rectangle   41.  42.         Canvas.drawtext ("Draw fan and Ellipse:", ten, +, p);  43.       /* Set the gradient color of this square is changed */  44.        Shader mshader = new LinearGradient ( 0, 0, 100,  45.                 New int[] {color.red, Color.green, Color.Blue, color.yellow,  46.                         Color.ltgray}, NULL, Shader.TileMode.REPEAT); A material that creates a linear gradient along a line.   47.        P.setshader (mshader);  48.        //P.setcolor (Color.Blue);  49.        RECTF oval2 = new RECTF (60, 100, 200, 240);//Set a new rectangle, scan measurement   50.        Canvas.drawarc (Oval2, ;  51.       //ARC, the first parameter is RECTF: The class is the second argument is the beginning of the angle, the third parameter is how many degrees, The fourth parameter is true when the fan is painted, is false when the arc is drawn  52.       //Draw oval, change oval   53.        Oval2.set (210,100,250,130);  54.        canvas.drawOval (Oval2, p);  55.  56.        Canvas.drawtext ("Draw triangle:", ten, p);  57.        //Draw this triangle, you can draw freeform   58.        path PATH = New Path ();  59.        Path.moveto (80, 200);//This point is the beginning of the polygon   60.         Path.lineto (+);  61.        Path.lineto (; ) 62.        path.close (); Make these points form a closed polygon   63.        canvas.drawpath (path, p);  64.  65.        //You can draw many freeform polygons, such as the following six shapes   66.       &nbSp P.reset ();//Reset   67.        p.setcolor (color.ltgray);  68.         P.setstyle (Paint.Style.STROKE);//Set hollow   69.         path path1=new path ();  70.        Path1.moveto (180, 200) ;  71.        Path1.lineto (a);  72.         Path1.lineto (;  73.        Path1.lineto (200), ;  74.        Path1.lineto (+);  75.         Path1.lineto (; ) 76.        path1.close () ;//closed   77.        canvas.drawpath (path1, p);  78.        /* 79.         * Path class package composite (paths for multi-contour Geometries 80.         * consists of straight line segments *, two curves, and three square curves, and can also be painted with oil paintings. DrawPath (path, paint), either filled or stroked 81.         * (Paint-based style), or can be used to cut or draw text in the path. 82.         */  83.           84.       //Draw rounded rectangles   85.        P.setstyle (Paint.Style.FILL);//Full   86.        p.setcolor (Color.ltgray);   87.        P.setantialias (TRUE);//Set the brush's Sawtooth effect   88.         Canvas.drawtext ("Draw rounded rectangles:", ten, 260, p);  89.        RECTF oval3 = new RECTF (80, 260, 200, 300);//Set up an additional rectangle   90.        Canvas.drawroundrect (OVAL3, PS, p);//The second parameter is the X-radius, the third parameter is the Y-radius   91.           92.       //Draw Bezier curves   93.        canvas.drawtext (" Draw Bezier Curve: ", ten, 310, p);  94.        p.reset ();  95.         P.setstyle (Paint.Style.STROKE);  96.        P.setcolor (color.green);  97.        path path2=new path ();  98.         Path2.moveto (100, 320);//Set the start of path   99.         Path2.quadto (150, 310, 170, 400); Set the control point coordinates and end point coordinates of the Bezier curve   100.        canvas.drawpath (path2, p);//Draw Bezier Curves   101.          102.        Draw points   103.        P.setstyle (Paint.Style.FILL);  104.         Canvas.drawtext ("Draw point:", ten, 390, p);  105.        canvas.drawpoint (390, p);//Draw a dot   106.         canvas.drawpoints (New float[]{60,400,65,400,70,400}, p);//Draw multiple points   107.           108.       //painting pictures, that is, stickers   109.        Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_launcher);  110.        canvas.drawBitmap (Bitmap, 250,360, p );  111.   }  112.}  

Android uses canvas to draw various shapes (points, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles)

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.