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, directly see the case
In this case, we are using a custom view class;

Customactivity.java

01.public class Customactivity extends Activity {
@Override.
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
. Init ();
07.}
08.
private void init () {
LinearLayout layout= (linearlayout) Findviewbyid (r.id.root);
Final Drawview view=new Drawview (this);
View.setminimumheight (500);
View.setminimumwidth (300);
14.//Notification View component Redraw
View.invalidate ();
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.
Public Drawview (Context context) {
A. Super (context);
05.}
06.
. @Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
10./*
11. * Method Description DrawRect Draw Rectangle Drawcircle Draw Circle DrawOval Draw Ellipse DrawPath draw freeform
* DrawLine draw a line drawpoin draw a point
13. */
14.//Create brushes
. Paint p = new paint ();
P.setcolor (color.red);//Set Red
17.
Canvas.drawtext ("Draw a circle:", ten, p);//Draw Text
Canvas.drawcircle (Ten, p);//Small Circle
P.setantialias (TRUE);//sets the brush's jagged effect. True is the removal, you see the effect will understand
Canvas.drawcircle (+, +, p);//Great Circle
22.
Canvas.drawtext ("Draw lines and arcs:", ten, A, p);
P.setcolor (Color.green);//Set Green
Canvas.drawline (max., Max., p);//Draw Line
Canvas.drawline (Max, Max, Max, p);//Slash
27.//Draw a smiley arc
P.setstyle (Paint.Style.STROKE);//Set Hollow
RECTF oval1=new RECTF (150,20,180,40);
Canvas.drawarc (Oval1, N, p);//small arcs
Oval1.set (190, 20, 220, 40);
Canvas.drawarc (Oval1, N, p);//small arcs
Oval1.set (160, 30, 210, 60);
Canvas.drawarc (oval1, 0, p);//small arcs
35.
Canvas.drawtext ("Draw rectangle:", ten, p);
PNS P.setcolor (Color.gray);//Set Grey
P.setstyle (Paint.Style.FILL);//Setup fills up
Canvas.drawrect (max., p);//Square
Canvas.drawrect (A, p);//Rectangle
41.
Canvas.drawtext ("Draw fan and Ellipse:", ten, +, p);
43./* Set the gradient color of this square is changed */
Shader Mshader = new LinearGradient (0, 0, 100, 100,
New int[] {color.red, Color.green, Color.Blue, Color.yellow,
Color.ltgray}, NULL, Shader.TileMode.REPEAT); A material that creates a linear gradient along a line.
P.setshader (Mshader);
//P.setcolor (Color.Blue);
RECTF oval2 = new RECTF (60, 100, 200, 240);//Set a new rectangle, sweep measurement
Canvas.drawarc (Oval2, p);
51.//Arc, the first parameter is RECTF: The class is the second parameter is the beginning of the angle, the third parameter is how many degrees, the fourth parameter is true when the fan, is false when the arc is drawn
52.//Draw oval, change oval
Oval2.set (210,100,250,130);
Canvas.drawoval (Oval2, p);
55.
Canvas.drawtext ("Draw triangle:", ten, p);
57.//Draw This triangle, you can draw freeform
*. path PATH = new Path ();
Path.moveto (80, 200);//This point is the starting point of the polygon
Path.lineto (120, 250);
Path.lineto (80, 250);
Path.close (); Make these points form a closed polygon
Canvas.drawpath (path, p);
64.
65.//You can draw many freeform polygons, such as the following six shapes
P.reset ();//Reset
P.setcolor (Color.ltgray);
P.setstyle (Paint.Style.STROKE);//Set Hollow
. Path path1=new path ();
Path1.moveto (180, 200);
Path1.lineto (200, 200);
Path1.lineto (210, 210);
Path1.lineto (200, 220);
Path1.lineto (180, 220);
Path1.lineto (170, 210);
Path1.close ();//Closed
Canvas.drawpath (path1, p);
78./*
The path class encapsulates the compound (the paths of the multi-contour geometries
80. * By Straight line segment *, two curve, and three square curve, can also be painted with oil painting. 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 Rectangle
P.setstyle (Paint.Style.FILL);//Full
P.setcolor (Color.ltgray);
P.setantialias (TRUE);//Set the brush's jagged effect
Canvas.drawtext ("Draw rounded rectangles:", ten, 260, p);
RECTF oval3 = new RECTF (80, 260, 200, 300);//Set a new rectangle
Canvas.drawroundrect (OVAL3, p);//The second parameter is the X-radius, the third parameter is the Y-radius
91.
92.//Draw Bezier Curve
Canvas.drawtext ("Drawing Bezier curve:", ten, 310, p);
94. P.reset ();
P.setstyle (Paint.Style.STROKE);
P.setcolor (Color.green);
. Path path2=new path ();
98. Path2.moveto (100, 320);//Set the starting point of path
Path2.quadto (150, 310, 170, 400); Set the control point coordinates and the end point coordinates of a Bezier curve
Canvas.drawpath (path2, p);//Draw a Bezier curve
101.
102.//Draw Points
103. P.setstyle (Paint.Style.FILL);
104. Canvas.drawtext ("Draw point:", ten, 390, p);
Canvas.drawpoint (390, p);//Draw a dot
106. Canvas.drawpoints (New float[]{60,400,65,400,70,400}, p);//Draw multiple points
107.
108.//Picture is a map
109. Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
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.