我试图使用下面的代码显示一个文本。
问题是文本没有水平居中。
当我为drawText设置坐标时,它将文本的底部设置在这个位置。我想要的文本被绘制,使文本也是水平居中。
这是一张进一步展示我的问题的图片:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//canvas.drawRGB(2, 2, 200);
Paint textPaint = new Paint();
textPaint.setARGB(200, 254, 0, 0);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTypeface(font);
textPaint.setTextSize(300);
canvas.drawText("Hello", canvas.getWidth()/2, canvas.getHeight()/2 , textPaint);
}
在我的例子中,我不需要把文本放在画布的中间,而是放在一个旋转的轮子上。尽管我必须使用以下代码才能成功:
fun getTextRect(textSize: Float, textPaint: TextPaint, string: String) : PointF {
val rect = RectF(left, top, right, bottom)
val rectHeight = Rect()
val cx = rect.centerX()
val cy = rect.centerY()
textPaint.getTextBounds(string, 0, string.length, rectHeight)
val y = cy + rectHeight.height()/2
val x = cx - textPaint.measureText(string)/2
return PointF(x, y)
}
然后我从View类调用这个方法:
private fun drawText(canvas: Canvas, paint: TextPaint, text: String, string: String) {
val pointF = getTextRect(paint.textSize, textPaint, string)
canvas.drawText(text, pointF!!.x, pointF.y, paint)
}
垂直对齐是困难的,因为文本下降和上升发生了,很多人使用Paint.getTextBounds()来检索TextWidth和TextHeight,但它不会使文本非常中心。这里我们可以使用Paint.measureText()来计算TextWidth, TextHeight我们简单地用下降和上升做减法,然后我们得到了最接近的TextSize,下面的工作对彼此来说都是相当容易的。
// the Paint instance(should be assign as a field of class).
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(getResources().getDimension(R.dimen.btn_textsize));
// the display area.
Rect areaRect = new Rect(0, 0, 240, 60);
// draw the background style (pure color or image)
mPaint.setColor(Color.BLACK);
yourCanvas.drawRect(areaRect, mPaint);
String pageTitle = "文字小说";
RectF bounds = new RectF(areaRect);
// measure text width
bounds.right = mPaint.measureText(pageTitle, 0, pageTitle.length());
// measure text height
bounds.bottom = mPaint.descent() - mPaint.ascent();
bounds.left += (areaRect.width() - bounds.right) / 2.0f;
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f;
mPaint.setColor(Color.WHITE);
yourCanvas.drawText(pageTitle, bounds.left, bounds.top - mPaint.ascent(), mPaint);
顺便说一下,我们强烈建议使用RectF而不是Rect,因为位置需要更准确的值,在我的经验,RectF做的顶部和底部偏差只有一个像素在xhdpi设备,Rect将是两个以上。
您的代码正在绘制文本基线的中心,在视图的中心。为了让文本在某个点(x, y)居中,你需要计算文本的中心,并把它放在这个点上。
这个方法将在点x, y居中绘制文本。如果你将它传递到视图的中心,它将以居中绘制文本。
private void drawTextCentered(String text, int x, int y, Paint paint, Canvas canvas) {
int xPos = x - (int)(paint.measureText(text)/2);
int yPos = (int) (y - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
canvas.drawText(text, xPos, yPos, textPaint);
}