当我在自定义视图中调用该函数canvas.drawText()
时,我得到了奇怪的结果,如下所示:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(50, 50);
mPaint.setTextSize(60);
String str = "helloworld";
float[] wids = new float[10];
mPaint.getTextWidths(str, wids);
float width = 0;
for (int j = 0; j < wids.length; j++) {
String string = String.valueOf(str.charAt(j));
canvas.drawText(string, width, 50, mPaint); //draw by characters
width = width + mPaint.measureText(string); //the start X
}
}
和这个:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(50, 50);
mPaint.setTextSize(60);
String str = "helloworld";
canvas.drawText(str, 0, 50, mPaint); // draw by strings
}
为什么这两种方法运行不同?我需要按字符绘制,但它的字距是错误的!有人可以指导我吗?