SoFunction
Updated on 2025-03-02

Introduction and methods of Canvas canvas for Android game development

Canvas, in English, the word means canvas. In Android, Canvas is used as a canvas, and as long as we use the set brush (Paint class) to draw anything we want on the canvas; in addition, it is also the core class for displaying bitmaps (Bitmap class). According to the user's preference, Canvas can also set some properties about the canvas, such as the color and size of the canvas. Canvas provides the following methods:
Canvas(): Create an empty canvas. You can use the setBitmap() method to set the drawing specific canvas.
Canvas(Bitmap bitmap): Create a canvas with a bitmap object, and then draw all the contents on the bitmap, so bitmap must not be null.
Canvas(GL gl): Used when drawing 3D effects, related to OpenGL.
drawColor: Set the background color of Canvas.
setBitmap: Set the specific canvas.
clipRect: Set the display area, that is, set the crop area.
isOpaque: Detect whether transparency is supported.
rotate: rotate canvas
translate: Move canvas

scale: Scale canvas
setViewport: Set the display window in the canvas.
skew:  Set the offset.

restore: used to restore the state before the last save

save: used to save the current state of Canvas

Note: The save method and restore method generally appear in pairs. 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 commonly used methods. In game development, we may need to perform rotation, zoom, and some other operations on a certain sprite. We can do this by rotating the canvas, but when rotating the canvas, all objects on the canvas will be rotated, and we only need to rotate one of them. At this time, we need to use the save method to lock the object that needs to be operated, and after the operation, we can unlock the lock through the restore method.


Get the canvas object

Copy the codeThe code is as follows:

Canvas canvas = getHolder().lockCanvas();

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

if (canvas != null) {
//-----Set the canvas drawing without jagging
    (pfd);
//------Use the filling canvas to swipe the screen
    ();
//----Draw text
    ("drawText", 10, 10, paint);
//-----Draw pixel points
    (10, 20, paint);
//----Draw multiple pixel points
    (new float[] { 10, 30, 30, 30 }, paint);
//----Draw a straight line
    (10, 40, 50, 40, paint);
//----Draw multiple straight lines
    (new float[] { 10, 50, 50, 50, 70, 50, 110, 50 }, paint);
//-----Draw the rectangle
    (10, 60, 40, 100, paint);
//-----Draw rectangle 2
    Rect rect = new Rect(10, 110, 60, 130);
    (rect, paint);
    (rect, paint);
//----Draw rounded rectangle
    RectF rectF = new RectF(10, 140, 60, 170);
    (rectF, 20, 20, paint);
//-----Draw a circle
    (20, 200, 20, paint);
//-----Draw arc shapes
    (new RectF(150, 20, 200, 70), 0, 230, true, paint);
//----Draw the ellipse
    (new RectF(150, 80, 180, 100), paint);
//----Draw the specified path diagram
    Path path = new Path();
//Set the path starting point
    (160, 150);
//Route 1
    (200, 150);
//Route 2
    (180, 200);
//The path ends
    ();
    (path, paint);
//----Draw the specified path diagram
    Path pathCircle = new Path();
//Add a circular path
    (130, 260, 20, );
//----Draw the path text with a circle
    ("PathText", pathCircle, 10, 20, paint);
}
 

Note: The above code is referenced from "Android Game Programming Starts From Zero"

Get a bitmap for custom image width and height (parameter 1: context object, parameter 2: resource ID, parameter 3: custom width, parameter 4: custom height)

Copy the codeThe code is as follows:

public static Bitmap loadBallView(Context context,int resId,int width,int height) {

    Resources resources = ();

    Drawable image = (resId);

    Bitmap bitmap = (width,height, .ARGB_8888);

         Canvas canvas = new Canvas(bitmap);
         (0, 0, width,height);
         (canvas);

         return bitmap;
    }

Draw bitmap using canvas
Copy the codeThe code is as follows:

Bitmap bitmap = loadBallView(context,,100,100);

Paint paint = new Paint();

(bitmap, startX, startY, paint); //Parameter 1: bitmap resource, parameter 2: start X coordinate, parameter 3: start Y coordinate, parameter 4: brush paint object