title: Live wallpaper canvas flickers when it includes a bitmap
body: <p>For my live wallpaper I use the following code (called by a Runnable) to draw each frame.  Each time it is called, I fill the current canvas with a solid color and draw a background bitmap (bg_image has been resized to perfectly fit the screen).  I then call drawParticles(c), which simply uses c.drawCircle(...) a bunch of times drawing particles all over the canvas.</p>
  
  <p>In the live wallpaper preview mode, this code works great.  However, when I actually set this as my live wallpaper it flickers and seems to not clear the canvas before drawing.  Let me 'splain:</p>
  
  <p>Frame 1: The bitmap is drawn and circles are overlaid.</p>
  
  <p>Frame 2: The bitmap is drawn and circles are overlaid (based on my rough understanding, there are two canvases that are drawn on and posted alternately for efficiency).</p>
  
  <p>Frame 3: The canvas is not being cleared!  This frame includes the new positions of each drawn circle <em>as well as</em> the circles from Frame 1!.</p>
  
  <p>Frame 4: Includes the new positions of each drawn circle as well as the circles from Frame 2!</p>
  
  <p>The end effect is that the circles leave "trails" all over the screen that flicker between (I believe) the two alternating canvases.  Why, based on my code below, isn't my canvas being cleared each frame?  Again, this works fine during preview mode but not when it is actually set as my live wallpaper.  It's also worth noting that this flickering problem only occurs if I am drawing a bitmap; if the background is just a solid color, this problem never arises.</p>
  
  <pre><code>final SurfaceHolder holder = getSurfaceHolder();
  
  Canvas c = null;
  try {
      c = holder.lockCanvas();
      if (c != null) {
          c.drawColor(Color.BLACK);
          c.drawBitmap(bg_image, 0, 0, null);
          fluid.drawParticles(c);
      }
  } finally {
      if (c != null) holder.unlockCanvasAndPost(c);
  }
  
  mHandler.removeCallbacks(mDrawRunnable);
  mHandler.postDelayed(mDrawRunnable, 1000/targetFramerate -(System.currentTimeMillis() - mLastTime));
  }
  </code></pre>