Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: SurfaceView ignoring postInvalidate()?

It's the first time I'm using this so I'm not sure whether its a bug in my code or I'm trying to do something thats not allowed.

Currently, I have a DrawThread() which does this in the run method.

try {
  c = m_surfaceHolder.lockCanvas(null);
  synchronized (m_surfaceHolder) {
    m_view.onDraw(c);
  }
}
finally {
  if (c != null) {
    m_surfaceHolder.unlockCanvasAndPost(c);
  }
}

While in this drawing thread (non UI-thread), my onDraw() method passes the canvas along to another method called drawScoreboard(). I wanted this method to have a short delay between displaying each row of the scoreboard, so I added a postInvalidateDelay(x) hoping it would trigger onDraw() again.

/**
 * A helper function which draws the scoreboard.
 */
private void drawScoreboard(Canvas canvas) {
  long delay = m_drawScoreboard.draw(canvas, m_paint);

  if (delay > 0) {
    this.postInvalidateDelayed(delay);
  }
}

The code executes correctly and postInvalidateDelayed() IS being called, but I'm baffled to why onDraw() is not being called. Are there any flags or initialisation I need to do in order to get this to work?

Any advice or theories are welcome. Thanks!

like image 646
twig Avatar asked Jul 29 '26 10:07

twig


1 Answers

You bloody legend Karan!

That pointed me in the right way. I found a few posts that mentioned it had to be done in the surfaceCreated() method rather than constructor and it got the postInvalidate() working.

However, enabling that would disable any drawing done by my custom DrawThread. Good thing is it should be easy to find a nice point in time to slot in that call.

Thanks again, View.setWillNotDraw() did the trick.

like image 114
twig Avatar answered Jul 31 '26 00:07

twig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!