"The zero start of Android game Programming" 12. Basics of game development (canvas canvas)

Source: Internet
Author: User
Tags drawtext

1.Canvas Canvas

Canvas class canvas encapsulates such things as graphics and picture drawing, and these commonly used functions are described below:

Drawcolor (int color)

Effect: Draw color overlay canvas, often used for brush screen

Parameters: Color values, also available in 16-in-form notation (ARGB)

DrawText (String text,float x,float y,paint Paint)

Function: Draw text characters

First parameter: text content

Second to third parameter: The x and y coordinates of the text

Fourth parameter: brush instance

Drawpoint (float x,float y,paint Paint)

Function: Draw pixel points

First to second parameter: the x and y coordinates of a pixel

Third parameter: brush instance

Drawpoints (float[] pts,paint Paint)

Function: Draw multiple pixel points

First parameter: Float array, where the x, y coordinates of multiple pixel points are placed in the array

Second parameter: brush instance

DrawLine (float startx,float starty,float stopx,float stopy,paint Paint)

Function: Draw a line

First two parameters: x, y coordinates of the starting point

The latter two parameters: the x, y coordinates of the end point

Last parameter: brush instance

DrawLines (float[] pts,paint Paint)

Function: Draw Multiple lines

First parameter: Float array, where the starting and ending points of multiple lines are placed, x, y coordinates

Second parameter: brush instance

DrawRect (Rect rect,paint Paint)

Function: Draw a rectangle

First parameter: instance of rectangle

Second parameter: brush instance

Drawroundrect (Rect rect,float x,float y,paint Paint)

function: Draw rounded rectangles

First parameter: instance of rectangle

Second parameter: radius of the X-axis of a fillet

Third parameter: radius of the y-axis of a fillet

Fourth parameter: brush instance

Drawcircle (float xc,float yc,float radius,paint Paint)

function: Draw a circle

First to second parameter: The center point of the Circle X, y coordinates

Third parameter: radius of a circle

Fourth parameter: brush instance

DrawArc (RECTF oval,float startangle,float sweepangle,boolean usecenter,paint Paint)

Function: Draw arc (sector)

First parameter: instance of rectangle

Second parameter: The starting angle of the arc (the default 45º is the starting angle of the graphic 0º)

Third parameter: End angle of Arc

The fourth parameter: whether to draw the center point, or if true, the starting point and the terminating point are respectively connected to the center point, thus forming a closed shape, and if False, the starting point directly connects the terminating point to form a closed shape

Fifth parameter: brush instance

DrawOval (RECTF oval,paint Paint)

Function: Draw ellipse

First parameter: instance of rectangle

Second parameter: brush instance

DrawPath (Path path,paint Paint)

Function: Draw the specified path graphic

First parameter: path instance

Second parameter: brush instance

Drawtextonpath (String text,path path,float hoffset,float voffset,paint Paint)

Function: Draws text along a specified path

First parameter: This article

Second parameter: path instance

Third parameter: The distance from which the text is drawn from the starting point

Fourth parameter: distance of text distance path

Fifth parameter: brush instance

Rect: A rectangular class that uses coordinates of two points to determine the size of a rectangle;

Its common constructors are:

Rect (float left,float top,float right,float bottom)

The first to second parameter represents the upper-left corner coordinate of the rectangle;

The third to fourth parameter represents the coordinates of the lower-right corner of the rectangle;

Android also provides a RECTF class, the main difference between the RECTF class and the Rect class is that the length unit accuracy is different, the RECTF uses a single-precision floating-point number, and Rect uses the int type; When you use Canvas to draw a rectangle, you can pass directly to the four parameters of the rectangle. You can also choose to pass in a rectangular instance.

Path: Specify the path to draw, then draw according to the route of its path, combining any required graphics.

Its usual functions are as follows:

MoveTo (float x,float y)

Function: Set the starting point of the path

Two parameters: coordinates of the starting point

LineTo (float x,float y)

Function: With the last end as the starting point, with this time the coordinate point as the end point, two points using a straight line connection

Two parameters: End position of this point line

Close ()

Function: the identification of the path end, if the point before the path is closed is not the starting point, will be automatically connected closed.

The above MoveTo, LineTo and close three functions are used together, the path start and end point only need to be set once, and the route LineTo can be set more than one.

Android.graphics.Path.quadTo (float x1,float y1,float x2,float y2)

function: Draw Bezier curves

First parameter: x-coordinate of the action point

Second parameter: The y-coordinate of the action point

Third parameter: The x-coordinate of the end point

Fourth parameter: The y-coordinate of the end point

2.Canvas Canvas Instance

As follows:

Step: New Project "Canvasproject", the game frame for the Mysurfaceview game framework, the specific steps refer to "11. Game Development Basics (Surfaceview game frame, View and Surfaceview differences)".

The main drawing Method Mydraw () is modified as follows:

  Public voidMydraw () {Try{Canvas=Sfh.lockcanvas (); if(canvas!=NULL)            {                //--Use fill canvas, brush screenCanvas.drawcolor (Color.Black); //--Draw textCanvas.drawtext ("DrawText", 10,15, paint); //--Draw pixel pointsCanvas.drawpoint (10,20, paint); //--Draw multiple pixel pointsCanvas.drawpoints (New float[]{10,30,30,30},paint); //--Draw a lineCanvas.drawline (10,40,50,40, paint); //--draw more than one lineCanvas.drawlines (New float[]{10,50,50,50,70,50,110,50},paint); //--Draw RectangleCanvas.drawrect (10,60,40,100, paint); //--Draw Rectangle 2Rect rect =NewRect (10,110,60,130);                Canvas.drawrect (Rect,paint); //--Draw rounded rectanglesRECTF RECTF =NewRECTF (10,140,60,170); Canvas.drawroundrect (RECTF,20,20, paint); //--Draw a circleCanvas.drawcircle (20,200,20, paint); //--Drawing arcsCanvas.drawarc (NewRECTF (150,20,200,70), 0,230,true, paint); //--Draw ellipseCanvas.drawoval (NewRECTF (150,80,180,100), paint); //--Draw the specified pathPath PATH =NewPath (); //set Path start pointPath.moveto (160,150); //Route 1Path.lineto (200,150); //Route 2Path.lineto (180,200); //End of PathPath.close ();                Canvas.drawpath (Path,paint); //--Draw the specified path graphPath pathcircle =NewPath (); //Add a circular frontal pathPathcircle.addcircle (130,260,20, Path.Direction.CCW); //--Draw a path text with a circleCanvas.drawtextonpath ("Pathtext", pathcircle,10,20, paint); }        }        Catch(Exception ex) {}finally {            if(canvas!=NULL) {sfh.unlockcanvasandpost (canvas); }        }    }

"The zero start of Android game Programming" 12. Basics of game development (canvas canvas)

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.