Path

The Path class holds a set of vector-drawing commands such as lines, rectangles, and curves. Here's an example that defines a circular path:

circle.addCircle(150, 150, 100, Direction.CW);

This defines a circle at position x=150, y=150, with a radius of 100 pixels. Now that we've defined the path, let's use it to draw the circle's outline plus some text around the inside:

private static final String QUOTE = "'Now is the time for all " + "'good men to come to the aid of their country."; canvas.drawPath(circle, cPaint);

canvas.drawTextOnPath(QUOTE, circle, 0, 20, tPaint);

Figure 4.1: Drawing text around a circle

You can see the result in Figure 4.1. Since the circle was drawn in the clockwise direction (Direction.CW), the text is also drawn that way.

If you want to get really fancy, Android provides a number of PathEffect classes that let you do things such as apply a random permutation to a path, cause all the line segments along a path to be smoothed out with curves or broken up into segments, and create other effects.

0 0

Post a comment