Search code examples
androidandroid-canvasinvalidation

Android: invalidate(dirty)


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?

enter image description here


Solution

  • 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.