Android Canvas With Examples

In this tutorial, we’ll be discussing a very important part of Android i.e. Canvas. It’s a territory which every developer prefers to stay away from. The reason for this tutorial is to make you more aware and at ease with the Android Canvas.

Android Canvas

Many times you end up in a scenario where you need to implement a custom View and/or animate it. It can happen that the requirements are so specific that you need to implement it from scratch. Canvas plays a vital role in building such Custom Views.

A Canvas is a 2D drawing framework that provides us with methods to draw on the underlying Bitmap. A Bitmap acts as the surface over which the canvas is placed. The Paint class is used to provide colors and styles.

Before we dig deep into Canvas, let’s look at the lifecycle of a Custom View.

A Custom View consists of the following commonly used methods:

Inside onMeasure we determine the size of the view and its children.

Inside onLayout the size is assigned to the view.

The onDraw method is where the canvas is drawn.

A Canvas Object basically comes as a parameter inside the onDraw method.

The invalidate() method is used to redraw the view. It calls the onDraw method again. Typically, this is used when the text or color or view needs to be updated based on certain events.

The Canvas class contains methods to draw certain shapes just as line, arc, circle, over. Moreover, we can draw complex geometry too using Paths.

We can also draw text or just simply Paint color on the canvas too.

Since canvas is drawn on the screen, views that are drawn need to be measured in terms of pixels (px).

Let’s look at how to draw basic stuff on Canvas through our sample android studio project.

Android Canvas Example Project Structure



Android Canvas Example Project

To create a Path, two methods are important: moveTo() and lineTo().

moveTo takes you to the coordinates you specify on the screen.

lineTo draws a line from the current position to the one specified.

close() is used to close a path.

How does the Canvas Coordinate System work?

In the canvas 0,0 is the top left point. As you move horizontally right the x coordinate increases.
As you move vertically down the y coordinate increases in pixels.

Following diagram illustrates the same:


Android Canvas Coordinate System

Rect vs RectF

Rect object is used to create a rectangle in which each side is specified in integer value.
RectF does the same but in float values.

In the following example, we’ll see how to draw the various shapes and also use Paint to style them.

Android Canvas Example Code

The code for the activity_main.xml layout is a basic root layout in which we’ll add our Canvas:

The code for the is given below:

The MyView class is where we create our Custom View. Let’s finally draw on the Canvas!

pxFromDp method is used to get the pixel equivalent of the dp value passed.

canvas.drawLine(250, 250, 400, 400, mPaint); simply draws a line from x1 y1 to x2 y2.

The following code is used to draw a rectangle at the center of the screen.

Inside the drawCircle method, the first two parameters passed are the coordinates of the center of the circle. The third parameter sets the radius for the circle.

clipRect clips the canvas with the rectangle. The last parameter sets the clipping region style.

Region.Op.DIFFERENCE sets the Paint on the region between the canvas and the rectangle specified in the clipRect method.

Inside the drawText method the two float values set the origin position of the text. We’ve set it at 30 percent from the left and 80 % from the top of the screen.

Why is Anti Alias flag needed in Paint?
An Anti alias flag ensures that the shape is smooth.

How are the different shapes drawn on the canvas positioned relative to each other?

Canvas Z-Order

Elements are arranged on top of each other in the order in which they are drawn. Similar to a frame layout based on the z-order.

The output of the above application is given below:



Android Canvas Basics Output 1


Android Canvas Basics Output 2

That brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply