1. Download our Official Android App: Forums for Android!

Apps Drawing Cache

Discussion in 'Android Development' started by ranxerox, Nov 19, 2009.

  1. ranxerox

    ranxerox Lurker
    Thread Starter
    Nov 19, 2009

    Nov 19, 2009
    I'm trying to understand how the drawing cache works. There don't seem to be a lot of examples. In fact, the only ones I can find use the cache as a way to acquire a bitmap of what's on the screen (e.g. for screen capture).

    From the documentation of View.getDrawingCache():

    To benefit from the cache, you must request the drawing cache by calling this method and draw it on screen if the returned bitmap is not null.
    This made me think that I had to override onDraw() to grab the contents of the cache and blt them to the screen myself. But when I tried to embed getDrawingCache() within onDraw() I got a stack overflow because buildDrawingCache() calls draw() which calls onDraw() which calls...you get the picture.

    Then I just turned on the drawing cache via setDrawingCacheEnabled() and added a trace statement elsewhere which showed the drawing cache bitmap created. I assume it is in use somehow, but I haven't been able to figure out where that occurs in the underlying Android framework source.

    Can someone explain this a little better than the existing documentation? Or perhaps give an example of its use that is not just acquiring the bitmap from a View?

    Why do I want to know this? Basically I want a two-layered display. The background (lower layer) is not static but changes slowly over time. The foreground (top layer) would change in real time due to user interaction. The lower layer would show the state of things and the upper layer would be how the user interacted to change the lower layer. Clear as mud, right?

    I could use a background image but that would require writing a bunch of code myself to handle it that way. I was thinking that perhaps I could piggyback on the existing caching mechanism. But I'm having trouble understanding how it works and where to hook into it. Perhaps I'm trying to make it do something it just can't do.

    In any event, a decent explanation of how the drawing cache works and what it is for would probably be of general value.

    A final note. There are apparently two caches. One is the "persistent" cache and seems to be connected to view animation and scrolling. The other is the "drawing" cache. I am interested in the latter.



Share This Page