目标:纯粹Canvas上的Android> = 1.6。如何垂直排列文本?
假设我想写一个函数,它将绘制一个(宽度,高度)大的红色矩形,然后在里面绘制一个黑色的文本。我希望文字在矩形的中心可视化。所以让我们尝试:
void drawHelloRectangle(Canvas c, int topLeftX,
int topLeftY, int width, int height) {
Paint mPaint = new Paint();
// height of 'Hello World'; height*0.7 looks good
int fontHeight = (int)(height*0.7);
mPaint.setColor(COLOR_RED);
mPaint.setStyle(Style.FILL);
c.drawRect(topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint);
mPaint.setTextSize(fontHeight);
mPaint.setColor(COLOR_BLACK);
mPaint.setTextAlign(Align.CENTER);
c.drawText("Hello World", topLeftX+width/2, ????, mPaint);
}
现在,我不知道要放什么的drawText的论证通过????
标记,即我不知道该怎么垂直对齐文本。
喜欢的东西
???? = topLeftY + height/2 + fontHeight/2 - fontHeight/8;
似乎工作或多或少好,但必须有更好的方法。
每次您在问题标题中放入* Android *时,小猫都会死亡,请不要这样做。谢谢。 – 2011-02-05 20:20:57