Android: invalidate(dirty)

不问归期 提交于 2019-12-20 20:32:03

问题


It seems like Android really doesn't like invalidate (Rect dirty), which is used to invalidate only part of a canvas. When I invalidate part of a canvas (shown in green below) and a ToggleButton outside of the canvas needs to be redrawn at the same time, the entire region shown in red is erased! It seems as though Android is just invalidating everything within the smallest rectangle encompassing the union of the two regions that need to be redrawn, even if one of the regions is outside of the View whose canvas I'm invalidating.

Is this standard behaviour, and if so, why would anyone use partial invalidation?


回答1:


Android has a class called ViewRootImpl. This class is owned by every window you see on the screen (the term window is a little confusing here, so for this explanation a window is the main activity's layout without any dialogs or popups on top). This layout is being traversed all the time, meaning that Android is just waiting to have a dirty rectangle for this window and draw it. Since this window may contain many views (buttons etc.) it goes through all of them and asks each one whether it needs redrawing. Each view returns a dirty rectangle to ViewRootImpl and all these rectangles are joined to one big rectangle that is redrawn in the end.

Why does it do that? well, the ViewRootImpl asks the WindowManagerService for one Canvas to draw on. This means that all views in one window actually share a Canvas everytime there is a traversal.

As to your question, if only one specific View had a dirty rectangle then only that dirty rectangle would be drawn, but since another view had one as well then the dirty rectangle contains both.




回答2:


This problem is caused by hardware acceleration. You must disable it in your activity in order to use invalidate(dirty rect). To disable the hardware acceleration in your activity open the manifest file and add:

android:hardwareAccelerated="false"

Now you can use the invalidate(dirty rect).



来源:https://stackoverflow.com/questions/14077008/android-invalidatedirty

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