Android Canvas.drawString display problem

1

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: http://img192.imageshack.us/img192/2782/imagexs.png

android
canvas
metrics
drawtext
asked on Stack Overflow Jun 7, 2010 by Arkaha

2 Answers

0

Hm, Try specifying particular x / y co-ords? with numbers rather than pre defined strings? give the ">>" different coordinates for it's draw space.

answered on Stack Overflow Jun 7, 2010 by Skizit
0

just add some space manually

c.drawText(text2, left + 2, top, mPaint);

or add a space char (" ") at start of text2

answered on Stack Overflow Jun 7, 2010 by zed_0xff

User contributions licensed under CC BY-SA 3.0