Android Canvas.drawString显示问题

发布于 2024-09-04 17:39:18 字数 1515 浏览 6 评论 0原文

我在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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2024-09-11 17:39:18

嗯,尝试指定特定的 x / y 坐标?使用数字而不是预定义的字符串?给出“>>”其绘制空间的不同坐标。

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

假扮的天使 2024-09-11 17:39:18

只需手动添加一些空格

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

或在 text2 开头添加一个空格字符 (" ")

just add some space manually

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文