问题描述
我将 Log.d() 调用放入扩展视图的 onDraw() 中,因此我可以查看它被调用的频率和时间.它在视图的实例化时被调用,这并不奇怪.但后来我注意到,它在 onTouchEvent() 处理的每次点击时都会被调用,即使我的代码没有做任何与图形相关的远程操作.但是,在视图的文档中,我似乎无法找到有关 onDraw() 实际上是被调用的.我并不真正关心我在这里的特定项目(这对我来说没有问题),我只想知道某处是否有列表或显示视图操作顺序的东西,特别是什么原因onDraw() 被调用.
I put a Log.d() call into the onDraw() of my extended View, so I could see how often and when it's getting called. It gets called upon instantiation of the view, which is not surprising. But then I notice, it gets called on every tap that is handled by onTouchEvent(), even though my code there isn't doing anything remotely related to graphics. However, in the documentation for Views, I can't seem to find anything about when onDraw() is actually called. I'm not really concerned about my particular project here (this doesn't cause a problem for me), I would just like to know if there is a list somewhere or something that shows the order of operations for a View, particularly what causes onDraw() to get called.
推荐答案
AFAIK,在以下情况下调用 View 的 onDraw():
AFAIK, a View's onDraw() is called when:
- 初始绘制视图
- 每当 invalidate() 被调用时在视图中
- The view is initially drawn
- Whenever invalidate() is called on the view
Invalidate 可以在需要时由您或系统调用.例如,许多视图会改变它们在触摸时的外观,例如 EditText 获得轮廓和光标,或者按钮处于按下状态.因此,视图会在触摸时重绘.
Invalidate can be called by you or the system whenever needed. For example, a lot of Views change how they look onTouch, like an EditText getting an outline and cursor, or a button being in the pressed state. Due to this, Views are redrawn on touch.
我同意最好有一份详细说明视图工作的文档,如果存在并且有人知道在哪里可以找到它,请告诉我们.
I agree that it would be nice to have a document that detailed the working of Views, and if one exists and somebody knows where to find it, please let us know.
这篇关于View.onDraw() --- 它什么时候被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!