If you think the Android project android-augment-reality-framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.jwetherell.augmented_reality.ui.objects;
//www.java2s.comimport android.graphics.Canvas;
import android.graphics.Color;
/**
* This class extends PaintableObject to draw text.
*
* @author Justin Wetherell <phishman3579@gmail.com>
*/publicclass PaintableText extends PaintableObject {
privatestaticfinalfloat WIDTH_PAD = 4;
privatestaticfinalfloat HEIGHT_PAD = 2;
private CharSequence text = null;
privateint color = 0;
privateint size = 0;
privatefloat width = 0;
privatefloat height = 0;
privateboolean bg = false;
public PaintableText(CharSequence text, int color, int size, boolean paintBackground) {
set(text, color, size, paintBackground);
}
/**
* Set this objects parameters. This should be used instead of creating new
* objects.
*
* @param text
* CharSequence representing this object.
* @param color
* Color of the object.
* @param size
* Size of the object.
* @param paintBackground
* Should the background get rendered.
* @throws NullPointerException
* if String param is NULL.
*/publicvoid set(CharSequence text, int color, int size, boolean paintBackground) {
if (text == null) thrownew NullPointerException();
this.text = text;
this.bg = paintBackground;
this.color = color;
this.size = size;
this.width = getTextWidth(text,0,text.length()) + WIDTH_PAD * 2;
this.height = getTextAsc() + getTextDesc() + HEIGHT_PAD * 2;
}
/**
* {@inheritDoc}
*/
@Override
publicvoid paint(Canvas canvas) {
if (canvas == null || text == null) thrownew NullPointerException();
setColor(color);
setFontSize(size);
if (bg) {
canvas.save();
canvas.translate(-width/2, -height/2);
setColor(Color.rgb(0, 0, 0));
setFill(true);
paintRect(canvas, x, y, width, height);
setColor(Color.rgb(255, 255, 255));
setFill(false);
paintRect(canvas, x, y, width, height);
canvas.restore();
}
paintText(canvas, (WIDTH_PAD-(width/2)), (HEIGHT_PAD+getTextAsc()-(height/2)), text, 0, text.length());
}
/**
* {@inheritDoc}
*/
@Override
publicfloat getWidth() {
return width;
}
/**
* {@inheritDoc}
*/
@Override
publicfloat getHeight() {
return height;
}
}