title: Smooth Painting in a SurfaceView
body: <p>I'm working on a paint-like app, but I've run it to some troubles.</p>
  
  <p>I know I have re-draw all the objects each frame, but this in turn makes performance slow further on when many objects are visible.</p>
  
  <p>I've also noticed that clearing the background only once, then adding objects to be drawn to the surface when painting is active makes the screen flash almost to the point of inducing epilepsy.</p>
  
  <p>So what is the best way to make a paint app 100% smooth no matter how many objects that are drawn?</p>
  
  <p>Epilepsy-inducing code:</p>
  
  <pre><code>public void run()
  {
      while (running)
      {
  
          if (!holder.getSurface().isValid())
              continue;
  
          if (drawObjectsToAdd.size() == 0)
              continue;
  
          drawing = true;
  
          Canvas c = holder.lockCanvas();
  
          if (redrawBackground)
          {
              c.drawARGB(255, 255, 255, 255);
              redrawBackground = false;
          }   
  
          drawObjects.addAll(drawObjectsToAdd);
          drawObjectsToAdd.clear();
  
          for (DrawObject draw : drawObjects)
          {
              draw.Draw(c, paint);
          }
  
          holder.unlockCanvasAndPost(c);
          drawing = false;
      }
  }
  </code></pre>
  
  <p>this adds a new object once it's been added outside of the thread, but it makes the screen flash so much it gives me headaches - and I only redraw the background once.</p>
  
  <p>Smooth at first but becomes laggy after a while, probably due to having to add hundreds and hundreds of objects in the end:</p>
  
  <pre><code>public void run()
  {
      while (running)
      {
          if (!holder.getSurface().isValid())
              continue;
  
          if (drawObjectsToAdd.size() &gt; 0)
          {
              drawObjects.addAll(drawObjectsToAdd);
              drawObjectsToAdd.clear();
  
              redraw = true;
          }
  
          if (clear)
          {
              drawObjectsToAdd.clear();
              drawObjects.clear();
              redraw = true;
              clear = false;
          }
  
          if (!redraw)
              continue;
  
          drawing = true;
  
          Canvas c = holder.lockCanvas();
  
          c.drawARGB(255, 255, 255, 255);
  
          for (DrawObject draw : drawObjects)
          {
              try
              {
                  draw.Draw(c, paint);
              }
              catch (Exception ex) { }
          }
  
  
          holder.unlockCanvasAndPost(c);
  
          drawing = false;
          redraw = false;
      }
  }
  </code></pre>
  
  <p>I, at least for this app wanna store all the objects that are added so it doesn't matter how it's painted as long as it's smooth all the way. Preferably, add a circle - it will render a new Bitmap on to the surface, instead of having to redraw lots of objects each frame - instead store them but do not add objects already drawn.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Pseudo-Code of how I want it to be:</p>
  
  <pre><code>If no background is drawn
     Draw background color
  If new items have been added
     Draw only new items to the background
     Store new items in objects list
  </code></pre>
  
  <p>This way, we'll only draw the background once. When a new item is added, only draw that item to the existing surface. When the objects increases, looping through every item will reduce performance greatly and it will not be pleasant to work with.</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <pre><code>private void Draw()
  {
      while (running)
      {
  
          if (!holder.getSurface().isValid())
              continue;
  
          if (picture == null)
          {
              picture = new Picture();
              Canvas c = picture.beginRecording(getWidth(), getHeight());
              c.drawARGB(255,  255,  255,  255);
              picture.endRecording();
          }
  
          if (drawObjectsToAdd.size() &gt; 0)
          {
              drawObjects.addAll(drawObjectsToAdd);
              drawObjectsToAdd.clear();
  
              Canvas c = picture.beginRecording(getWidth(), getHeight());
  
              for (DrawObject draw : drawObjects)
              {
                  draw.Draw(c, paint);
              }
  
              picture.endRecording();
              drawObjects.clear();
  
          }
  
          Canvas c2 = holder.lockCanvas();
          c2.drawPicture(picture);
  
          holder.unlockCanvasAndPost(c2);
      }
  }
  </code></pre>
  
  <p>This last method from <strong>Update 2</strong> makes it render all the lines like the "Snake game" when adding circles. Looks like a moving snake on a background, where some of it's circles disappear one frame and others don't the next. If I skip to redraw each frame, it will instead vary which of these circles that are visible.</p>