Circle

A circle can be drawn by specifying a center point (x and y) and radius. The following code will be rendered as shown in Figure 4-6.

paint.setColor(Color.GREEN);

paint.setStyle(Paint.Style.STROKE);

float radius = 20;

canvas.drawCircle(x, y, radius, paint); 31 Slffl® 3:19 PM

Figure 4-6. Circle

Path

A path is a series of lines that can be used to create an arbitrary shape. To draw a path, we first have to construct a Path object. The Path object can have any number of calls telling it to move to a point without drawing, using moveTo, or draw a line to a point using lineTo. Of course, there are methods for drawing arcs and so on. Documentation of these methods can be found in the documentation of the Path class at http://developer.android.com/reference/android/graphics/Path.html.

The Path can then be passed to the Canvas method drawPath.

Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.GREEN); Path p = new Path();

// Without the first "moveTo", drawing will start at (0,0)

canvas.drawPath(p, paint);

Of course, we aren't limited to just drawing lines, shapes, and points. We can draw text on the Canvas as well, using the method drawText; we simply pass in the text to draw as

Android Canvas Moveto Lineto
Figure 4-7. Path

Was this article helpful?

0 0

Post a comment