Code example for Paint

Methods: getTextBounds

0
 
	// Draw text aligned correctly. 
	private void drawText(Canvas canvas, int i, String text, Paint p,
			boolean left) {
		Rect bounds = new Rect();
		p.getTextBounds(text, 0, text.length(), bounds);
 
		float y = (float) (height / 2.0 - i * bounds.height() * 1.2)
				- (float) bounds.height() * 0.3f;
 
		if (left)
			canvas.drawText(text,
					(float) (-width / 2.0 + bounds.height() * .2f), y, p);
		else 
			canvas.drawText(
					text,
					(float) (width / 2.0 - bounds.width() - bounds.height() * .2f),
					y, p);
 
	}