Android Canvas.drawString display problem
Posted
by Arkaha
on Stack Overflow
See other posts from Stack Overflow
or by Arkaha
Published on 2010-06-07T09:36:36Z
Indexed on
2010/06/07
9:42 UTC
Read the original article
Hit count: 517
Hello everyone!
I encounter this problem when displaying text on SurfaceView, some chars can climb up on others, code is here:
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);
}
In the case of a second text Tom>> all displayed correctly, but the first text Evi>> not. The problem is that the chars >> draws in Evi draw space(last char "i")!! It is possible to see if you zoom the picture, what am I doing wrong and how to fix this?
screen shot can be found here:
© Stack Overflow or respective owner