Take the 2-minute tour ×

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

No, it will not cause a stack overflow.

share|improve this answer
add comment

When you call invalidate() you can assume that the onDraw(canvas c) is going to be called, yes.

But be careful with calling the nextFrame inside the onDraw(), be sure to control the refreshing time.

If you want to implement the onDraw(Canvas c) method just override it and you can draw there whatever you want.

If you want to control the refreshing time based on the current time:

Inside the method 1 you said you were controlling the current time:

//I assume you instantiate the lasTTime in the     onCreate()

void method1() {
    while (currentTime - lastTime < N) {
        currentTime = //call the current time as you do
    lastTime = currentTime;
    //call method2
share|improve this answer
I know that calling invalidate() causes onDraw() to be called, but I'm concerned that it might call it directly and result in a nest like invalidate()->onDraw()->invalidate()->onDraw() etc leading to a stack overflow (the exception not the website). The idea is to control refreshing time based on when drawing is complete. How would you control it? –  Andrew Wyld Jun 29 '12 at 9:41
If you Measure the time (method 1 of your description) you can compare the previous time and the current time to refresh every X miliseconds just doing a while in your first method (I'm going to edit my answer with that) –  Miquel Coll Jun 29 '12 at 9:43
Yes, but it seems to me that presupposes a known frame rate for a device. Isn't there a way to receive an event from a View that tells you the drawing cycle is complete? I had assumed that all drawing functions in onDraw() occur on the UI thread, so presumably at the end of onDraw() the drawing cycle IS complete? –  Andrew Wyld Jun 29 '12 at 9:49
Since the onDraw() is the end of the cycle and in the end you call the first method you can assume the drawing is done, yes. So the fps would be different but controlled. –  Miquel Coll Jun 29 '12 at 9:54
It's now working beautifully. –  Andrew Wyld Jun 29 '12 at 16:17
show 1 more comment

Your Answer


