android中自定义视图的onDraw方法中TextView的setText
我的布局中有一个自定义视图,并且布局中还有一个 TextView,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:text="Tiny" android:orientation="vertical">
<my.package.drawView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView android:text="Application Name" android:id="@+id/appName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30px" android:textStyle="italic" />
</LinerLayout>
我在活动的 onCreate 方法中设置对 TextView 的引用,并在自定义视图的 onDraw 方法中更改文本TextView如下:
public void onDraw(Canvas canvas) {
tv.setText("player score: "+score);
}
但是在ondraw方法中设置文本会导致其进入无限循环。 有人可以告诉我如何在调用自定义视图的 onDraw 方法时或之后更新 TextView 的文本吗?
I have a custom view in my layout and I also have a TextView in the layout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:text="Tiny" android:orientation="vertical">
<my.package.drawView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView android:text="Application Name" android:id="@+id/appName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30px" android:textStyle="italic" />
</LinerLayout>
I set the reference to the TextView in the onCreate method of the activity and in the onDraw method of my custom view, I change the text of the TextView as follows:
public void onDraw(Canvas canvas) {
tv.setText("player score: "+score);
}
But setting the text in the ondraw method causes it to go in an infinite loop.
Could someone please tell me how do I update the text of the TextView in or after the onDraw method of the custom view is called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您更改当前视图中的视觉事物时,即本例中的文本视图。整个可见区域被重新绘制,并且作为 my.package.drawView 的一部分被告知绘制自身,从而导致无限循环。
应避免更改 onDraw 中的视觉属性,这样的设计是不正确的。
也许您需要某种计时器来更新分数或在游戏中发生某些情况时增加分数的其他地方?
When you change a visual thing in the current view i.e. textview in this case. The entire visible region is redrawn and as part of that my.package.drawView is told to draw itself and hence the infinite loop.
Changing the visual properties in onDraw should be avoided and the design is incorrect.
Maybe you need some sort of timer to update the score or somewhere else that increases the score when something happens in the game?
首先你为什么要这么做?
在 TextView 的情况下,一旦您更改文本,就会调用 On Draw。
调用 setText 将在内部调用 invalidate,这最终将导致再次调用 onDraw。
如果您只想处理文本更改,请实现 TextChangeListner 并完成所需的工作。
不要把这些东西放在 onDraw 中...onDraw 功能应该用于玩转画布。
如果您真的想在 onDraw 中工作,请使用
canvas.drawText(String text, float x, float y, Paint Paint)
更多参考:http://developer.android.com/reference/android/graphics/Canvas.html
Firstly why you wanna do that?
On Draw in case of TextView will be called as soon as you change the text..
Calling setText will internally call invalidate which will finally result in call of onDraw again.
In case you want to handle just the text change then implement a
TextChangeListner
and do the need full.Don't put this stuff in onDraw... onDraw functionality should be used to play around with canvas.
In case you really wanna work in onDraw, use
canvas.drawText(String text, float x, float y, Paint paint)
more reference : http://developer.android.com/reference/android/graphics/Canvas.html
当你说它进入无限循环时,你到底是什么意思?我建议您确保您的自定义视图确实按照您的预期重新绘制。一个简单的方法是记录您在 textView 中更新的所有内容:
此日志多久打印一次?
What exactly do you mean when you say it goes into infinite loop? I suggest you ensure that your custom view is indeed being redrawn as you expect it to. An easy way to do that would be to log whatever you are updating in the textView:
How often does this log get printed?
你会得到一个无限循环,因为 setText 调用 onDraw,而 onDraw 又调用 setText。
如果仅在文本发生更改时调用 setText,则可以避免这种情况:
You get an infinite loop because setText calls onDraw, and onDraw calls setText.
You can avoid this if you call setText only if the text has changed: