Android Canvas Drawing

Source: Internet
Author: User
Tags drawtext

public class Drawview extends View {public Drawview (context context) {
Super (context);
} @Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
/*
* Method Description DrawRect Draw Rectangle Drawcircle Draw Circle DrawOval Draw Ellipse DrawPath draw freeform
* DrawLine Draw line Drawpoin draw point
*/
Create a brush
Paint p = new paint ();
P.setcolor (color.red);//Set Red Canvas.drawtext ("Draw circle:", ten, p);//Draw Text
Canvas.drawcircle (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 Canvas.drawtext ("Draw line and Arc:", ten, A, p);
P.setcolor (Color.green);//Set Green
Canvas.drawline (Max, Max, Max, p);//Draw Line
Canvas.drawline (Max, Max, Max, p);//Slash
Draw a smiley curve
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, N., false, p);//Small Curved Canvas.drawtext ("Draw rectangle:", ten, p);
P.setcolor (Color.gray);//Set Gray
P.setstyle (Paint.Style.FILL);//Setup fills up
Canvas.drawrect (max., p);//Square
Canvas.drawrect (, p),///Rectangle Canvas.drawtext ("Draw fan and Ellipse:", ten, +, p);
/* 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 up an additional rectangle, scanning
Canvas.drawarc (Oval2, p);
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
Draw the oval and change the oval.
Oval2.set (210,100,250,130);
Canvas.drawoval (Oval2, p); Canvas.drawtext ("Draw triangle:", ten, p);
Draw this triangle and you can draw any polygon
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); You can draw a lot of freeform polygons, like the six shapes below
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);
/*
* Path class encapsulates composite (paths of multi-contour geometries
* By line segment *, two times curve, and three square curve, can also be painted with oil painting. DrawPath (path, paint), either filled or stroked
* (Paint-based style), or can be used to cut or draw text in the path.
*/

Draw rounded rectangles
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 fresh rectangle
Canvas.drawroundrect (OVAL3, p);//The second parameter is the X-radius, the third parameter is the Y-radius

Draw Bezier Curves
Canvas.drawtext ("Drawing Bezier curve:", ten, 310, p);
P.reset ();
P.setstyle (Paint.Style.STROKE);
P.setcolor (Color.green);
Path path2=new path ();
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

Draw points
P.setstyle (Paint.Style.FILL);
Canvas.drawtext ("Draw point:", ten, 390, p);
Canvas.drawpoint (390, p);//Draw a dot
Canvas.drawpoints (New float[]{60,400,65,400,70,400}, p);//Draw multiple points

A picture is a map
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
Canvas.drawbitmap (Bitmap, 250,360, p);
}
}

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.