为什么我调用 Canvas.drawText() 不起作用

发布于 2024-09-28 13:47:10 字数 397 浏览 4 评论 0原文

大家好: 我正在编写一个继承自 TextView 的类,并重写其 onDraw() 方法,但在该方法中,我对 canvas.drawText() 的调用似乎没有要工作,代码如下:

protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(android.graphics.Color.WHITE);
    paint.setTextSize(20);

    String text = "hello";
    canvas.drawText(text, 0, 0, paint);
}

Hi all:
I'm writing a class that inherit from TextView, and override its onDraw() method, but in the method, my invoke of canvas.drawText() doesn't seems to work, the code just like below:

protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(android.graphics.Color.WHITE);
    paint.setTextSize(20);

    String text = "hello";
    canvas.drawText(text, 0, 0, paint);
}

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

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

发布评论

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

评论(2

人间☆小暴躁 2024-10-05 13:47:10

它没有绘制任何东西,因为文本坐标位于左下角。由于您尝试在 0,0 上绘制,因此它将在屏幕上方绘制。

尝试将最后一行更改为:

canvas.drawText(text, 0, 20, paint);

It isn't drawing anything because the text coordinates are bottom left. Since you're trying to draw on 0,0, it will draw above the screen.

Try changing the last line to:

canvas.drawText(text, 0, 20, paint);
冧九 2024-10-05 13:47:10

到处都是很好的建议,干得好,伙计们真的。不过,下次如果你在评论或其他问题中询问这个人,在将其作为答案发布之前,他是否尝试过完全明显的方法,那就太好了。你真的认为当他到达不起作用的地步时,他就直接来到 Stack Overflow 而不进行实验吗?

我确实有一个替代建议,足够疯狂的是基于整个问题,而不仅仅是在没有太多实际知识的情况下可以回答的部分。

我建议在不在 TextView 子类中的 Canvas 上尝试您的 drawText 调用,因为这样它就不会被 TextView 中管理其可绘制状态的数百行代码覆盖。

Excellent suggestions all around, great job guys really. Next time though it would be nice if you ask the guy in a comment or something whether or not he's tried the completely obvious before posting it as an answer. Do you really think that the second he got to a point that wasn't working he just came straight to Stack Overflow without experimenting?

I do have an alternate suggestion, that crazily enough is based on the entire question and not just the part that could be answered without much actual knowledge.

I would recommend trying your drawText call on a Canvas that's not in a TextView subclass as that way it won't be overridden by the several hundred lines of code in TextView that manage it's drawable state.

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