Introduction and method of Canvas canvas for Android game development _android

Source: Internet
Author: User
Tags drawtext

Canvas, in English, the word means canvas. In Android, Canvas is used as a canvas, so long as we can draw anything we want on the canvas with the help of a set brush (Paint Class), it is also the core class for displaying bitmaps (bitmap classes). With the user's preferences, canvas can also set some properties on the canvas, such as the color of the canvas, size and so on. Canvas provides some of the following methods:
Canvas (): Creates an empty canvas, and you can use the SetBitmap () method to set the drawing specific canvas.
Canvas (Bitmap Bitmap): Creates a canvas with Bitmap object, draws the content to Bitmap, so Bitmap must not be null.
Canvas (GL GL): Used when drawing a 3D effect, associated with OpenGL.
Drawcolor: Sets the background color of the canvas.
SetBitmap: Set the specific canvas.
Cliprect: Set the display area, that is, set the cropping area.
Isopaque: Detects whether transparency is supported.
Rotate: Rotate the canvas
Translate: Moving the canvas

Scale: Scale canvas
SetViewport: Sets the display window in the canvas.
Skew: Sets the offset.

Restore: Used to restore the state before the last save

Save: For saving the current state of the canvas

Note: The Save method, the Restore method, is typically in pairs, and the Save method can be more than the restore method, but the restore method cannot be more than the Save method

Several common methods are listed above. In game development, we may need to rotate, scale, and perform some other actions on a wizard. We can do this by rotating the canvas, but rotating the canvas rotates all the objects on the canvas, and we just need to rotate one of them, and then we need to use the Save method to lock the object that needs to be manipulated, and then unlock it by the Restore method after the operation.


Get Canvas Canvas Object

Copy Code code as follows:

Canvas Canvas = Getholder (). Lockcanvas ();

Some of the properties and methods of the canvas and some applications
Copy Code code as follows:

if (canvas!= null) {
----Set Canvas drawing without sawtooth
Canvas.setdrawfilter (PFD);
----Use a padded canvas to brush the screen
Canvas.drawcolor (Color.Black);
----Draw Text
Canvas.drawtext ("DrawText", ten, paint);
----to draw pixel points
Canvas.drawpoint (A, paint);
----Draw multiple pixel points
Canvas.drawpoints (New float[] {Ten, A, paint);
----Draw a line
Canvas.drawline (M, n, paint);
----Draw Multiple lines
Canvas.drawlines (New float[] {Ten, A, a, a, a, a, a, a, paint);
----Draw a rectangle
Canvas.drawrect (A, n, m, paint);
----Draw a Rectangle 2
Rect Rect = new Rect (10, 110, 60, 130);
Canvas.drawrect (rect, paint);
Canvas.drawrect (rect, paint);
To draw a rounded rectangle----
RECTF RECTF = new RECTF (10, 140, 60, 170);
Canvas.drawroundrect (RECTF, paint);
----Draw a circle
Canvas.drawcircle (paint);
----Draw an arc
Canvas.drawarc (new RECTF, 0, 230, true, paint);
----Draw an Ellipse
Canvas.drawoval (New RECTF (), 180, paint);
----Draw the specified path graph
Path PATH = new Path ();
Set the path start point
Path.moveto (160, 150);
Route 1
Path.lineto (200, 150);
Route 2
Path.lineto (180, 200);
End of Path
Path.close ();
Canvas.drawpath (path, paint);
----Draw the specified path graph
Path pathcircle = new Path ();
Add a circle to the path
Pathcircle.addcircle (130, Path.Direction.CCW);
----Draw path text with a circle
Canvas.drawtextonpath ("Pathtext", pathcircle, paint);
}

Note: The above code refers to the beginning of the Android game programming from scratch

Get the custom picture width high bitmap (parameter one: Context object, parameter two: Resource ID, parameter three: custom width, parameter four: custom height)

Copy Code code as follows:

public static Bitmap Loadballview (context context,int resid,int width,int height) {

Resources of resources = context.getresources ();

drawable image = Resources.getdrawable (RESID);

Bitmap Bitmap = Bitmap.createbitmap (Width,height, Bitmap.Config.ARGB_8888);

Canvas Canvas = new Canvas (bitmap);
Image.setbounds (0, 0, width,height);
Image.draw (canvas);

return bitmap;
}

Using canvas to draw bitmap
Copy Code code as follows:

Bitmap Bitmap = Loadballview (context,r.drawable.image,100,100);

Paint Paint = new Paint ();

Canvas.drawbitmap (Bitmap, StartX, starty, paint); Parameter one: bitmap resource, Parameter two: start x coordinate, parameter three: start y coordinate, parameter four: Brush Paint Object

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.