title: Background image taking too long to draw (Canvas) Jerky Sprites......?
body: <p>Hey all I'm at a crossroads with my app that I've been working on.</p>
  
  <p>It's a game and an 'arcade / action' one at that, but I've coded it using Surfaceview rather than Open GL (it just turned out that way as the game changed drastically from it's original design).</p>
  
  <p>I find myself plagued with performance issues and not even in the game, but just in the first activity which is an animated menu (full screen background with about 8 sprites floating across the screen).</p>
  
  <p>Even with this small amount of sprites, I can't get perfectly smooth movement. They move smoothly for a while and then it goes 'choppy' or 'jerky' for a split second.</p>
  
  <p>I noticed that (from what I can tell) the background (a pre-scaled image) is taking about 7 to 8 ms to draw.  Is this reasonable? I've experimented with different ways of drawing such as:</p>
  
  <pre><code>    canvas.drawBitmap(scaledBackground, 0, 0, null);
  </code></pre>
  
  <p>the above code produces roughly the same results as:</p>
  
  <pre><code>    canvas.drawBitmap(scaledBackground, null, screen, null);
  </code></pre>
  
  <p>However, if I change my holder to:</p>
  
  <pre><code>    getHolder().setFormat(PixelFormat.RGBA_8888);
  </code></pre>
  
  <p>The the drawing of the bitmap shoots up to about 13 MS (I am assuming because it then has to convert to RGB_8888 format.</p>
  
  <p>The strange thing is that the rendering and logic move at a very steady 30fps, it doesn't drop any frames and there is no Garbage Collection happening during run-time.</p>
  
  <p>I've tried pretty much everything I can think of to get my sprites moving smoothly</p>
  
  <p>I recently incorporated interpolation into my gameloop:</p>
  
  <pre><code>    float interpolation = (float)(System.nanoTime() + skipTicks - nextGameTick)
                / (float)(skipTicks);
  </code></pre>
  
  <p>I then pass this into my draw() method:</p>
  
  <pre><code>    onDraw(interpolate)
  </code></pre>
  
  <p>I have had some success with this and it has really helped smooth things out, but I'm still not happy with the results.</p>
  
  <p>Can any one give me any final tips on maybe reducing the time taken to draw my bitmaps or any other tips on what may be causing this or do you think it's simply a case of Surfaceview not being up to the task and therefore, should I scrap the app as it were and start again with Open GL?</p>
  
  <p>This is my main game loop:</p>
  
  <pre><code>int TICKS_PER_SECOND = 30;
  int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
  int MAX_FRAMESKIP = 10;
  
  long next_game_tick = GetTickCount();
  int loops;
  
  bool game_is_running = true;
  while( game_is_running ) {
  
      loops = 0;
      while( GetTickCount() &gt; next_game_tick &amp;&amp; loops &lt; MAX_FRAMESKIP) {
          update_game();
  
          next_game_tick += SKIP_TICKS;
          loops++;
      }
      interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
                      / float( SKIP_TICKS );
      display_game( interpolation );
  }
  </code></pre>
  
  <p>Thanks</p>