开发者

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 initia开发者_高级运维lisation I need to do in order to get this to work?

Any advice or theories are welcome. Thanks!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜