efficient android rendering
        Posted  
        
            by 
                llll
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by llll
        
        
        
        Published on 2011-07-01T15:04:09Z
        Indexed on 
            2012/11/21
            17:00 UTC
        
        
        Read the original article
        Hit count: 297
        
I've read quite a few tutorials on game programming on android, and all of them provide basically the same solution as to drawing the game, that is having a dedicated thread spinning like this:
public void run() { 
    while(true) { 
        if(!surfaceHolder.getSurface().isValid()) continue; 
        Canvas canvas = surfaceHolder.lockCanvas(); 
        drawGame(canvas); /* do actual drawing here */
        surfaceHolder.unlockCanvasAndPost(canvas); 
    }
} 
now I'm wondering, isn't this wasteful? Suppose I've a game with very simple graphics, so that the actual time in drawGame is little; then I'm going to draw the same things on and on, stealing cpu from the other threads; a possibility could be skipping the drawing and sleeping a bit if the game state hasn't changed, which I could check by having the state update thread mantaining a suitable status flag. But maybe there are other options. For example, couldn'it be possible to synchronize with rendering, so that I don't post updates too often? Or am I missing something and that is precisely what lockCanvas does, that is it blocks and burns no cpu until proper time?
Thanks in advance
L.
© Stack Overflow or respective owner