title: How to freeze the last frame of an animation in a SurfaceView?
body: <p>I'm drawing on a SurfaceView using a thread that has the following calssic run() method:</p>
  
  <pre><code>@Override
  public void run() {
      while (mRun) {
          Canvas canvas = null;
          synchronized (mSurfaceHolder) {
                  try {
                      canvas = mSurfaceHolder.lockCanvas();
                      update();
                      doDraw(canvas);
                  } finally {
                      if (canvas != null) {
                          mSurfaceHolder.unlockCanvasAndPost(canvas);
                      }
                  }
          }
      }
  }
  </code></pre>
  
  <p>I wanted to save some work if the next frames that are about to be drawn from that moment on, are exactly the same as the previous one, so I skipped the onDraw, but than I got <a href="https://stackoverflow.com/questions/6847323/android-surfaceview-canvas-flickering-after-trying-to-clear-it">flickering issues due to the double buffering mechanism</a>.
  Then I went on and did something like this:</p>
  
  <pre><code>@Override
  public void run() {
      while (mRun) {
          Canvas canvas = null;
          synchronized (mSurfaceHolder) {
              if (mIsAnimating) {          // &lt;---- Look ma! A flag!!!
                  try {
                      canvas = mSurfaceHolder.lockCanvas();
                      update();
                      doDraw(canvas);
                  } finally {
                      if (canvas != null) {
                          mSurfaceHolder.unlockCanvasAndPost(canvas);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>If I turn off the mIsAnimating flag, thus stopping my run loop from locking the canvas and drawing on it, everything worked like a charm - I did some animation and after it was finished, I could freeze the drawing cycle and keep the animation's last frame on screen.</p>
  
  <p>But then I had to go and see the following from <a href="http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas%28%29" rel="nofollow noreferrer">Android docs</a>:</p>
  
  <blockquote>
    <p>The content of the Surface is never preserved between unlockCanvas()
    and lockCanvas(), for this reason, every pixel within the Surface area
    must be written. The only exception to this rule is when a dirty
    rectangle is specified, in which case, non-dirty pixels will be
    preserved.</p>
  </blockquote>
  
  <p>So... how come my code works?</p>