为什么我调用 Canvas.drawText() 不起作用
大家好: 我正在编写一个继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它没有绘制任何东西,因为文本坐标位于左下角。由于您尝试在 0,0 上绘制,因此它将在屏幕上方绘制。
尝试将最后一行更改为:
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:
到处都是很好的建议,干得好,伙计们真的。不过,下次如果你在评论或其他问题中询问这个人,在将其作为答案发布之前,他是否尝试过完全明显的方法,那就太好了。你真的认为当他到达不起作用的地步时,他就直接来到 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.