LabelViewpublic class LabelView extends android.view.View Example of how to write a custom subclass of View. LabelView
is used to draw simple text views. Note that it does not handle
styled text or right-to-left writing systems. |
Fields Summary |
---|
private android.graphics.Paint | mTextPaint | private String | mText | private int | mAscent |
Constructors Summary |
---|
public LabelView(android.content.Context context)Constructor. This version is only needed if you will be instantiating
the object manually (not from a layout XML file).
super(context);
initLabelView();
| public LabelView(android.content.Context context, android.util.AttributeSet attrs)Construct object, initializing with any attributes we understand from a
layout file. These attributes are defined in
SDK/assets/res/any/classes.xml.
super(context, attrs);
initLabelView();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LabelView);
CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
// Retrieve the color(s) to be used for this view and apply them.
// Note, if you only care about supporting a single color, that you
// can instead call a.getColor() and pass that to setTextColor().
setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
a.recycle();
|
Methods Summary |
---|
private final void | initLabelView()
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16);
mTextPaint.setColor(0xFF000000);
setPadding(3, 3, 3, 3);
| private int | measureHeight(int measureSpec)Determines the height of this view
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
| private int | measureWidth(int measureSpec)Determines the width of this view
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
| protected void | onDraw(android.graphics.Canvas canvas)Render the text
super.onDraw(canvas);
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
| protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
| public void | setText(java.lang.String text)Sets the text to display in this label
mText = text;
requestLayout();
invalidate();
| public void | setTextColor(int color)Sets the text color for this label.
mTextPaint.setColor(color);
invalidate();
| public void | setTextSize(int size)Sets the text size for this label
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
|
|