【发布时间】:2011-09-29 00:53:51
【问题描述】:
我正在尝试使用 Paint 在 Canvas 上绘制文本,但我似乎无法找出合适的 x 和 y 来给出它。如果我记录 getWidth 和 getHeight,我可以看到 View 实际上给出了旋转画布的宽度和高度。因此,它类似于 20 X 240。我所拥有的很接近,但我只希望文本垂直和水平居中。
关于如何做到这一点的任何想法?
这是现在的样子:
这是我的代码:
public class VerticalText extends View {
private Paint mTextPaint;
private String mText;
private int mAscent;
public ListLabelView(Context context) {
super(context);
initListLabel();
this.setBackgroundColor(Color.BLUE);
}
private final void initListLabel() {
mTextPaint = new Paint();
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(12);
mTextPaint.setColor(Color.WHITE);
setPadding(3, 3, 3, 3);
invalidate();
}
/**
* sets the text to display in this label
* @param text
*/
public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}
/**
* sets the text color for this label
* @param color
*/
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureHeight(widthMeasureSpec), measureWidth(heightMeasureSpec));
}
/**
* Determines the width of this view
* @param measureSpec A measureSpec packed into an int
* @return The width of the view, honoring constraints from measureSpec
*/
private int measureWidth(int measureSpec) {
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;
}
/**
* Determines the height of this view
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
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;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.rotate(-90, getWidth()/2, getHeight()/2);
//
mTextPaint.measureText(mText);
Log.v("width x height:", getWidth() + " X " + getHeight());
canvas.drawText(mText, (getWidth() - (getWidth())) - 50, getHeight()/2, mTextPaint);
super.onDraw(canvas);
}
}
【问题讨论】:
标签: android