我在 SurfaceView 上显示文本时遇到这个问题,一些字符可以爬上其他字符,代码在这里:
private static void fakeDraw(Canvas c)
{
Paint mPaint = new Paint();
int color = 0xff000000;
mPaint.setColor(color);
mPaint.setStrokeWidth(2);
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
FontMetricsInt fm = mPaint.getFontMetricsInt();
int fh = Math.abs(fm.top);
int left = 0;
int top = 100;
Rect smallClip = new Rect(left, top-fh, left + 200, top + 30);
Rect bigClip = new Rect(0, 0, getW(), getH());
c.drawRect(bigClip, mPaint);
String text1 = "Evi";
String text2 = ">>";
String text3 = "Tom";
color = 0xff303030;
mPaint.setColor(color);
c.drawRect(smallClip, mPaint);
color = 0xffffffff;
mPaint.setColor(color);
c.drawText(text1, left, top, mPaint);
Rect bounds = new Rect();
mPaint.getTextBounds(text1, 0, text1.length(), bounds);
left += bounds.width();
c.drawText(text2, left, top, mPaint);
left -= bounds.width();
top += 12;
c.drawText(text3, left, top, mPaint);
mPaint.getTextBounds(text3, 0, text3.length(), bounds);
left += bounds.width();
c.drawText(text2, left, top, mPaint);
}
在第二个文本 Tom>> 的情况下全部正确显示,但第一个文本 Evi>> 不正确。问题是字符 >> 在 Evi 绘图空间中绘制(最后一个字符“i”)!可以查看您是否缩放图片,我做错了什么以及如何解决这个问题?
屏幕截图可以在这里找到:http: //img192.imageshack.us/img192/2782/imagexs.png