在这个方法文档中写道:
x The x-coordinate of origin for where to draw the text
y The y-coordinate of origin for where to draw the text
但它没有说明这个文本的方向。我知道文本是从原点开始绘制的,但是当我给出以下参数时,我的文本会被删除:
canvas.drawText(displayText, 0, canvas.getHeight(), textPaint);
此外,假设我正在使用Align.LEFT(意味着文本被绘制在x,y原点的右侧)
那么正确的参数应该是什么(假设我不想使用固定数字)?
答案 0 :(得分:3)
这是我最终使用的:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (textAlignment == Align.CENTER) {
canvas.drawText(displayText, canvas.getWidth()/2, canvas.getHeight()-TEXT_PADDING, textPaint);
}
else if (textAlignment == Align.RIGHT) {
canvas.drawText(displayText, canvas.getWidth()-TEXT_PADDING, canvas.getHeight()-TEXT_PADDING, textPaint);
}
else if (textAlignment == Align.LEFT) {
canvas.drawText(displayText, TEXT_PADDING, canvas.getHeight()-TEXT_PADDING, textPaint);
}
//canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), p);
}
两条评论:
答案 1 :(得分:2)
也许您可以使用以下代码段来查看其是否正常工作:
int width = this.getMeasuredWidth()/2;
int height = this.getMeasuredHeight()/2;
textPaint.setTextAlign(Align.LEFT);
canvas.drawText(displayText, width, height, textPaint);
在我的情况下,宽度和高度是任意计算的。