Android Programming Canvas Draws a variety of graphics (dots, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles) _android

Source: Internet
Author: User
Tags drawtext

This example describes the canvas of Android programming to draw various graphics. Share to everyone for your reference, specific as follows:

1, first of all say 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 in which you can draw a lot of things;

We can interpret this canvas as a piece of memory that the system provides to us (but in fact it's just a set of paint APIs, the real memory is the bitmap), and it provides a whole range of ways to manipulate the memory area, all of which are paint APIs. That is to say that in this way we have been able to draw a stroke or use graphic to paint what we need, what to draw and what to show are controlled by ourselves.

This way is divided into two kinds according to the environment: One is the use of ordinary view canvas drawing, there is a special surfaceview canvas to paint. The main difference between the two is that you can define a specific thread in the Surfaceview to complete the drawing, and the application does not need to wait for the view brush to improve performance. The previous one is suitable for processing the small, the frame rate is smaller than the animation, for example chess game and so on, the latter one mainly uses in the game, the high quality animation aspect drawing.

The following are common methods for canvas classes:

DrawRect (RECTF rect, Paint Paint)//Draw area, parameter one for RECTF one area
DrawPath (path path, Paint Paint)//Draw a path, parameter one is path object
Drawbitmap (Bitmap Bitmap, Rect src, Rect DST, Paint Paint)/map, parameter one is our regular Bitmap object, parameter two is the source region (this is Bitmap), and parameter three is the target region ( should be in canvas position and size), parameter four is paint brush object, because the use of scaling and stretching the possibility, when the original rect is not equal to the target RECT performance will have a significant loss.
DrawLine (float startx, float starty, float stopx, float stopy, paintpaint)//Draw line, parameter one starting point x axis position, parameter two start point's Y axis position, parameter three endpoint's X axis horizontal position, Parameter four is the y-axis vertical position, 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)//Render text, canvas class In addition to the above can also depict text, parameter one is String type text, parameter two x axis, parameter three Y axis, Parameter four is a paint object.
DrawOval (RECTF Oval, Paint Paint)//Draw ellipse, parameter one is scanning 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 three is radius, parameter four is Paint object;
DrawArc (RECTF oval, float startangle, float sweepangle, Boolean usecenter, Paint Paint)//Arc painting,
Parameter one is the RECTF object, the boundary of a rectangular region is used to define the shape, size, arc, parameter two is the starting angle (degree) at the beginning of the arc,
Parameter three-scan angle (degree) begins to be measured clockwise, parameter four is if this is true, include an arc of the center of the ellipse, and close it, if it is false it will be an arc, and the parameter five is the Paint object;

Also understand a paint class:

Class Overview

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

The Paint class has style and color information about how to draw geometry, text, and bitmaps.
Paint represents the brushes, brushes, pigments, etc. on the canvas;

Paint Class Common methods:

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 is 0~255
Setantialias (Boolean AA)//anti-aliasing
SetColor (int color)//Set color, here the Android definition has a color class that contains some common color definitions
Settextscalex (float ScaleX)//Set text scaling multiples, 1.0f to original
Settextsize (float textsize)//Set Font size
Setunderlinetext (Booleanunderlinetext)//Set Underline

2, directly to see the case

Look at the effect chart:

In this case, we used a custom view class:

Customactivity.java

public class Customactivity extends activity { 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    Super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.main); 
    Init (); 
  } 
  private void Init () { 
    linearlayout layout= (linearlayout) Findviewbyid (r.id.root); 
    Final Drawview view=new Drawview (this); 
    View.setminimumheight (); 
    View.setminimumwidth (); 
    Notifies the view component to redraw the  
    view.invalidate (); 
    Layout.addview (view); 
  } 


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, dots, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles, and so on!

Drawview.java

public class Drawview extends View {public Drawview (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 straight 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 sawtooth effect of the brush. 
    True is the removal, we see the effect on the understanding of the canvas.drawcircle (A, A, p);//Great Circle Canvas.drawtext ("Draw line and Arc:", ",", p); P.setcolor (Color.green)//Set green Canvas.drawline (p);//Draw Line Canvas.drawline (A, 190, p); 
    /Slash/Draw smiley face arc P.setstyle (Paint.Style.STROKE);//Set Hollow RECTF oval1=new RECTF (150,20,180,40); 
    Canvas.drawarc (OVAL1, 180, 180, false, p);//Small Arc Oval1.set (190, 20, 220, 40); 
    Canvas.drawarc (OVAL1, 180, 180, false, p);/small arcOval1.set (160, 30, 210, 60); 
    Canvas.drawarc (oval1, 0, 180, false, p);//Small Arc canvas.drawtext ("Draw rectangle:", ten, p); 
    P.setcolor (Color.gray);/Set up Grey P.setstyle (Paint.Style.FILL);/set to fill up Canvas.drawrect (p, q); square 
    Canvas.drawrect (160, p); rectangular canvas.drawtext ("fan-shaped and oval:", N, p); /* Setting gradient color of this square is changed/Shader Mshader = new LinearGradient (0, 0, MB, new int[] {color.red, color.gr Een, 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 a novel rectangle, scan the measurement canvas.drawarc (Oval2, 130, True, 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 painting sector, is a false time to draw arcs/draw an ellipse, the oval to change the Oval2.set (210,100,250,130); 
    Canvas.drawoval (Oval2, p); 
    Canvas.drawtext ("Draw triangle:", p); Drawing this triangle, you can draw a 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); 
    You can draw a lot of freeform polygons, such as the following six-P.reset (); Reset P.setcolor (Color.ltgray); 
    P.setstyle (Paint.Style.STROKE);/Set the 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 the composite (the path of the multi-contour geometry is made from a straight line segment *, a two-time curve, and a three-time curve, which can also be painted in oil painting.) 
     DrawPath (path, paint), either filled or stroked * (based on the paint style), or can be used to cut or draw text in the path. 
    *///Draw rounded corner rectangle P.setstyle (Paint.Style.FILL);//Full P.setcolor (Color.ltgray); 
    P.setantialias (TRUE);//Set the sawtooth effect of the brush canvas.drawtext ("Draw Rounded Rectangle:", ten, p); 
    RECTF oval3 = new RECTF (80, 260, 200, 300);//Set up a novel rectangle Canvas.drawroundrect (Oval3, N, p);//The second parameter is the X radius and the third parameter is the Y radius 
   To draw a Bezier curve Canvas.drawtext ("Draw Bezier Curves:", 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 the path Path2.quadto (150, 310, 170, 400); 
    Set the control point coordinates and the endpoint coordinates of the Bezier curve Canvas.drawpath (path2, p);//Draw a Bezier curve/point P.setstyle (Paint.Style.FILL); 
    Canvas.drawtext ("Draw point:", ten, 390, p); 
    Canvas.drawpoint (390, p)//Draw a point canvas.drawpoints (new float[]{60,400,65,400,70,400}, p);//Draw multiple dots//pictures, that's the map. 
    Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); 
  Canvas.drawbitmap (Bitmap, 250,360, p);

 } 
}

I hope this article will help you with your Android programming.

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.