android view viewgroup invalidate ondraw and draw

核能气质少年 提交于 2019-12-12 14:50:08

问题


We're trying to write our own MapView, and I'm trying to figure out how adding overlays to the mapview causes them to get drawn in other mapping APIs

I've got a MapView that extends ViewGroup. I think I've figured out that calling MapView.invalidate() causes the mapview's dispatchDraw method to be called. Does that sound correct?

If that's true, when is the onDraw and draw method of the mapview called?

More importantly is all this view and which methods called when stuff documented well anywhere?

Thanks!

EDIT This SO post explained that for classes that extend ViewGroup, the onDraw method is not called automatically. You have to force it if you need it. But as ebarrenchea pointed out, the order is draw, onDraw, dispatchDraw IF all the methods are called


回答1:


Calling invalidate on your viewgroup will force draw to run which will in turn call onDraw and then dispatchDraw. You should have a look at the view documentation here and the View source code here for more information.




回答2:


invalidate() has to be called from UI thread to cause onDraw(). Try to use postInvalidate() which should have the same effect as invalidate(), but works from non UI threads.



来源:https://stackoverflow.com/questions/15013445/android-view-viewgroup-invalidate-ondraw-and-draw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!