Android uses canvas to draw a variety of graphics

Source: Internet
Author: User
Tags drawtext

Canvas is popular as a canvas, and we can use brush paint to draw arbitrary graphics on top of it.


Principle:

You can think of canvas as an avatar or an interface to your surface, drawing on your surface. The canvas encapsulates all the draw calls. Through canvas,

The content that is drawn to surface is first stored in a memory area (that is, the corresponding Bitmapz), and the bitmap is eventually rendered to the window.


Use:

1, canvas can be directly new canvas ();

2, rewrite the OnDraw () method in the view, there is a canvas, the content discussed today.


Method:

Plot area, parameter one is RECTF one area

DrawRect (RECTF rect, paint paint)

Draw a path, parameter one to path object

DrawPath (path path, paint paint)

//Map, parameter one is our regular bitmap object, parameter two is the source area (here is bitmap), parameter three is the target area

(should be in the canvas's position and size), parameter four is the paint brush object, because it is possible to scale and stretch, when the original

Rect is not equal to the target rect when there is a significant loss in performance.

Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint)

Draw the line, the x-axis position of the starting point of the parameter, the y-axis position of the parameter two starting point, the x-axis horizontal position of the parameter three endpoints,

Parameter four the vertical position of the y-axis, and the last parameter is the paint brush object.

DrawLine (float StartX, float starty, float stopx, float stopy, Paintpaint)

Draw point, parameter one horizontal x axis, parameter two vertical y axis, third parameter is paint object.

Drawpoint (float x, float y, paint paint)

Render 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.

DrawText (String text, float x, floaty, paint paint)

Draw ellipse, parameter one is scan area, parameter two is Paint object;

DrawOval (RECTF oval, paint paint)

The circle is drawn, the parameter is the x-axis of the center point, the second parameter is the y-axis of the center point, the parameter is radius, and the parameter is the Paint object;

drawcircle(float cx, float CY, float radius,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

Starting angle (degrees) at the beginning of the arc, the parameter three-sweep angle (degree) began to measure clockwise, parameter four is if this is true, including

Oval Center arc, and close it, if it is false it will be an arc, parameter five is the Paint object;

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


The next step is to start drawing. Also need tools Paint,path

Paint, is the brush, we new paint (); Wait for a brush, and then set some of its properties.

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

SetStyle (Style.stroke)//Set Brush Hollow


Directly on the example:

public class Drawviewactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Set Drawview ' s width and heightdisplay d = Getwindowmanager (). Getdefaultdisplay ( ;D Rawview dv = new Drawview (this);d v.width = D.getwidth ();d v.height = D.getheight (); Setcontentview (DV);} Class Drawview extends View {public float width;public float height;private Paint mpaint;public drawview (context context) {Super (context); mpaint = new Paint (); Mpaint.setcolor (color.red); Mpaint.setantialias (true);} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas);// Drawtextcanvas.save (mpaint.settextsize); Canvas.drawtext ("Hello Honjane", "a", "mpaint"); Canvas.restore ();// Drawrectcanvas.save (); RECTF r = new RECTF (+,--), Mpaint.setcolor (Color.Blue); Canvas.drawrect (R, Mpaint); Canvas.restore (); Canvas.save (); Mpaint.setcolor (Color.Blue); Canvas.drawrect (n, N, a, mpaint); Canvas.restore ();//DrawCirclecanvas.save (); Mpaint.setstyle (Style.stroke); Canvas.drawcircle (WIDTH/2, HEIGHT/2, Mpaint); Canvas.restore ();//Drawarccanvas.save (); RECTF oval1 = new RECTF (n/A, 600), Canvas.drawarc (Oval1,, +, False, mpaint);//Small curved Oval1.set (300, 300,, 780); Canvas.drawarc (Oval1, V, N, M, false, Mpaint), Oval1.set (n., 780) Canvas.drawarc (oval1 paint); Canvas.restore ();//triangle Canvas.save (); Mpaint.setstyle (Style.fill); Path p = new Path ();p. MoveTo;p. LineTo (+);p. LineTo (+);p. Close (); Canvas.drawpath (P, mpaint); Canvas.restore (); Canvas.save (); Mpaint.setstyle (Paint.Style.FILL);//Full Mpaint.setcolor (Color.ltgray); Mpaint.setantialias (TRUE);//sets the sawtooth effect of the brush canvas.drawtext ("Draw rounded rectangles:", ten, 260, mpaint); RECTF oval3 = new RECTF (80, 260, 200, 300);//Set a new Rectangle Canvas.drawroundrect (OVAL3, X, R, Mpaint);//The second parameter is a radius, the third parameter is the Y-radius CA Nvas.restore ();//discoloration of the canvas.save (); Shader Shader = new LinearGradient (0, 0, +, +, new int[] {color.black, Color.cyan, CoLor. Dkgray, Color.gray}, Null,shader.tilemode.mirror); Mpaint.setshader (Shader); RECTF oval2 = new RECTF (+, +, +), Canvas.drawarc (Oval2, Max, N, True, mpaint); Canvas.restore ();//Picture CANVAS.SA ve ();      Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);      Canvas.drawbitmap (Bitmap, 250,360, mpaint); Canvas.restore ();}}}





Related Article

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.