7.1 Draw with a Canvas in Android

7.1.1 Introduction to Android Canvas

Framework of android provides 2D APIs which we can render on to screen. For 2D graphics we usually opt for any of the two following options:

  1. Graphics or animation object is drawn into View object from layout.
  2. We can draw graphics directly onto the canvas.

Android Canvas class encapsulates the bitmaps used as surface. It exposes the draw methods which can be used for designing. Let us first clear the following terms:

  • Bitmap: The surface being drawn on.
  • Paint: It lets us specify how to draw the primitives on bitmap. It is also referred to as “Brush”.
  • Canvas: It supplies the draw methods used to draw primitives on underlying bitmap.

Each drawing object specifies a paint object to render. Let us see the available list of drawing objects and they are as follows:

  1. drawArc: This draws an arc between the two angles bounded by an area of rectangle.
  2. drawBitmap: It draws an bitmap on canvas.
  3. drawRGB/drawARGB/drawColor: This fills the canvas with a single color.
  4. drawBitmapMesh: It draws a bitmap using a mesh. It manipulates the appearance of target by moving points on it.
  5. drawCircle: This draws a circle on a specified radius centered on a given point.
  6. drawLine(s):it draws a line (or series of lines) between points.
  7. drawOval: it draws an oval which is bounded by the area of rectangle.
  8. drawPaint: It fills the entire canvas with a specific paint.
  9.  drawPath: It draws a path as per specification.
  10. drawPicture: It draws a picture specified on a rectangular area.
  11. drawPosText: it draws a text string specifying the offset of each character.
  12. drawRect: It draws a rectangle.
  13. drawRoundRect: it draws a rectangle with round edges.
  14. drawText: It draws a text string on canvas.

The Paint class consists of a paint brush and a palette. It lets us choose how to render the primitives drawn into canvas by draw methods. We can control the color, style, font, special effects etc can be modified by modifying the paint object. For instance, setColor method can be used to select the color of Paint. Paint class supports transparency so it can be used to control variety of shades or effects, etc. Let us create a simple example and see the basic usage of canvas and paint.

7.1.2 Android Canvas Example

Open your IDE and create a project. Name it as you like. I am naming it CanvasEx application. You don’t have to modify your xml file at all. For reference cross check with following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >
        android:text="@string/hello_world" />

                                            Figure - activity_main.xml file of Android canvas example

Now open the main activity file and code it as shown in the following listing:

package com.android.tution.Graphics;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new CanvasView(this));
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

                                            Figure -  main activity file Android canvas example

Create another class and name it CanvasView.java and it is going to be the canvas of your application. You don’t need any corresponding xml file for this. Just copy any image in your drawable folder so that it can be referenced in the code.So code it as shown in the following listing:

package com.android.tution.Graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
public class CanvasView extends View {
    public CanvasView(Context context) {
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
    private void drawRotateImage(Canvas canvas) {
        // TODO Auto-generated method stub
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
        bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
        Matrix matrix = new Matrix();
        matrix.postTranslate(200, 180);
        canvas.drawBitmap(bitmap, matrix, null);
    private void drawCirle(Canvas canvas) {
        // TODO Auto-generated method stub
        Paint paint = new Paint();
        canvas.drawCircle(50, 60, 50, paint);
        canvas.drawCircle(200, 60, 50, paint);

                                                                      Figure - CanvasView Class of  Android canvas example

As you always do please cross check the manifest file with the following listing:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
        android:targetSdkVersion="18" />
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

                                                                Figure -  manifest file of Android canvas example

Now you are done with coding part. Create a suitable emulator and run the application. Output should be similar to the following snapshot:

Output of CanvasEx app

Figure - Output of CanvasEx app

CongratulationsJ buddies!!! We are done with canvas. Hope you enjoyed this tutorial. Keep practicing. See you in the next section. Happy App Developing!!!