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, Map inflateParams)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, inflateParams);
initLabelView();
Resources.StyledAttributes a = context.obtainStyledAttributes(attrs,
R.styleable.LabelView);
CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
int textColor = a.getColor(R.styleable.LabelView_textColor, 0);
if (textColor != 0) {
setTextColor(textColor);
}
int textSize = a.getInt(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);
mPaddingLeft = 3;
mPaddingTop = 3;
mPaddingRight = 3;
mPaddingBottom = 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()) + mPaddingTop
+ mPaddingBottom;
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) + mPaddingLeft
+ mPaddingRight;
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, mPaddingLeft, mPaddingTop - 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();
|
|