Canvas for Android Game Development

Source: Internet
Author: User
Tags drawtext android games

Canvas. In English, this word indicates Canvas. In Android, Canvas is used as a Canvas. We can draw anything we want on the Canvas with the help of the Set Paint class; it is also the core class of the Bitmap class. You can also set Canvas attributes, such as the color and size of the Canvas. Canvas provides the following methods:
Canvas (): Create an empty Canvas. You can use setBitmap () to set the Canvas to be drawn.
Canvas (Bitmap bitmap): If you create a Canvas with a bitmap object, the content is drawn on bitmap. Therefore, bitmap cannot be null.
Canvas (GL gl): used to draw 3D effects. It is related to OpenGL.
DrawColor: Specifies the background color of the Canvas.
SetBitmap: Specifies the canvas.
ClipRect: Set the display area, that is, set the cropping area.
IsOpaque: checks whether Transparency is supported.
Rotate: rotate the canvas
Translate: Move the canvas

Scale: Zoom the canvas
SetViewport: Set the display window in the canvas.
Skew: Set the offset.

Restore: used to restore the status before the last save

Save: used to save the current status of the Canvas.

Note: The save method and restore method are usually paired. The save method can be more than the restore method, but the restore method cannot be more than the save method.

The above lists several common methods. In game development, we may need to perform rotation, scaling, and other operations on a certain genie. We can achieve this by rotating the canvas, but when we rotate the canvas, all objects on the canvas will be rotated, and we just need to rotate one of them, in this case, you need to use the save method to lock the objects to be operated and use the restore method to unlock the objects after the operation.

Get the canvas object

Copy codeThe Code is as follows: Canvas canvas = getHolder (). lockCanvas ();

Canvas attributes, methods, and applicationsCopy codeThe Code is as follows: if (canvas! = Null ){
// ---- Set the canvas drawing to be non-sawtooth
Canvas. setDrawFilter (pfd );
// ---- Use the canvas to fill the screen
Canvas. drawColor (Color. BLACK );
// ---- Draw text
Canvas. drawText ("drawText", 10, 10, paint );
// ---- Draw the pixel
Canvas. drawPoint (10, 20, paint );
// ---- Draw multiple pixels
Canvas. drawPoints (new float [] {10, 30, 30, 30}, paint );
// ---- Draw a straight line
Canvas. drawLine (10, 40, 50, 40, paint );
// ---- Draw multiple straight lines
Canvas. drawLines (new float [] {10, 50, 50, 50, 70, 50,110, 50}, paint );
// ---- Draw a rectangle
Canvas. drawRect (10, 60, 40,100, paint );
// ---- Draw a rectangle 2
Rect rect = new Rect (10,110, 60,130 );
Canvas. drawRect (rect, paint );
Canvas. drawRect (rect, paint );
// ---- Draw a rounded rectangle
RectF rectF = new RectF (10,140, 60,170 );
Canvas. drawRoundRect (rectF, 20, 20, paint );
// ---- Draw a circle
Canvas. drawCircle (20,200, 20, paint );
// ---- Draw an arc
Canvas. drawArc (new RectF (150, 20,200, 70), 0,230, true, paint );
// ---- Draw an ellipse
Canvas. drawOval (new RectF (150, 80,180,100), paint );
// ---- Draw the specified path Image
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 );
// The path ends.
Path. close ();
Canvas. drawPath (path, paint );
// ---- Draw the specified path Image
Path pathCircle = new Path ();
// Add a circular path
PathCircle. addCircle (130,260, 20, Path. Direction. CCW );
// ---- Draw the path text with a circle
Canvas. drawTextOnPath ("PathText", pathCircle, 10, 20, paint );
}
 

NOTE: For the above Code, refer to "programming for Android games from scratch".

Get the bitmap with the width of the custom image (parameter 1: context object, parameter 2: Resource ID, parameter 3: Custom width, parameter 4: Custom height)

Copy codeThe Code is as follows: public static Bitmap loadBallView (Context context, int resId, int width, int height ){

Resources 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;
}

Use canvas to draw bitmapCopy codeThe Code is as follows: Bitmap bitmap = loadBallView (context, R. drawable. image, 100,100 );

Paint paint = new Paint ();

Canvas. drawBitmap (bitmap, startX, startY, paint); // parameter 1: bitmap resource, parameter 2: Start X coordinate, parameter 3: Start Y coordinate, parameter 4: paint object

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.