Search code examples
androidstack-overflowondrawinvalidation

Will calling invalidate() from onDraw() cause a stack overflow?


After much struggling, I have determined that a SurfaceView won't do what I want it to do, namely, be above some UI elements and below others. I've also discovered that drawing all the UI elements on to the SurfaceView is actually significantly slower than the multi-layered hierarchy I was trying to replace. Therefore, I am trying to code my own animated View using onDraw().

The simplest method I can think of for this is roughly as follows:

  1. Measure the time now
  2. Perform calculations about the animation state
  3. Call invalidate()
  4. Draw the animation at onDraw()
  5. Return to 1.

I already have a nextFrame() function from my SurfaceView which does steps 1 and 2, and the draw function is actually trivial—a single path. I could make nextFrame() call invalidate() and I could make onDraw() call nextFrame() to signal that the drawing has completed and we are ready for the next frame. Unfortunately, I can imagine this causing problems.

Does invalidate() directly call onDraw()? Will I need to implement a calling thread as in SurfaceView?


Solution

  • No, it will not cause a stack overflow.