O'Reilly logo

Building Android UIs with Custom Views by Raimon Ràfols Montané

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drawing text

Text can be considered a primitive from the point of view of Canvas operations, but we've put it here on its own section, as it's quite important. Instead of starting with the simplest example, as we've just introduced Paths, we'll continue our previous example and draw the text on top of the Path. To draw the text, we'll reuse the Paint object for the Bezier curve, but we'll add some text parameters:

pathPaint.setTextSize(50.f); 
pathPaint.setTextAlign(Paint.Align.CENTER); 

This sets the size of the text and also aligns it to the center of the Path, so every time we add new points, the text position will adapt to remain at the center. To draw the text, we simply call the drawTextOnPath() method:

canvas.drawTextOnPath("Building ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required