title: How can I save and load each bitmap after drawing, instead of re-drawing each stroke every time
body: <p>I'm working on a drawing application in Android/Java, and at the moment I re-draw every stroke instead of just loading the Bitmap. I'm thinking it will be a good idea to store a bitmap after each stroke and just load that onto the Canvas, but I'm not sure how to go about doing that.</p>
  <p>This is what I have so far:</p>
  <pre><code> protected void touchStart(float x, float y) {
          mPath.reset();
          mPath.moveTo(x, y);
          mX = x;
          mY = y;
   }
  
      protected void touchMove(float x, float y) {
          float dx = Math.abs(x - mX);
          float dy = Math.abs(y - mY);
          if (dx &gt;= TOUCH_TOLERANCE || dy &gt;= TOUCH_TOLERANCE) {
              mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
              mX = x;
              mY = y;
          }
      }
  
      protected void touchUp() {
          mPath.lineTo(mX, mY);
  
          mMyPathsDrawn.add(new Pair&lt;&gt;(mPath, mPaint));
  
          // Kill this so we don't double draw
          mPath = new Path();
  
          mPaint = new Paint(mPaint);
      }
  
  
      protected void onDraw() {
          if (mCanvas == null) {
              return;
          }
          mCanvas.drawBitmap(mBgBitmap, null, mSurfaceHolder.getSurfaceFrame(), null);
          for (Pair&lt;Path, Paint&gt; p : mPathsToDraw) {
              mCanvas.drawPath(p.first, p.second);
          }
          mCanvas.drawPath(mPath, mPaint);
          Canvas surfaceCanvas = mSurfaceHolder.lockCanvas();
          surfaceCanvas.drawBitmap(mBitmap, 0, 0, null);
          mSurfaceHolder.unlockCanvasAndPost(surfaceCanvas);
      }
  
  
  </code></pre>
  <p>The workflow is: touchStart/touchMove/touchUp get called, and then onDraw gets called each time after each method.</p>
  <p>If I am thinking correctly, I want to save mBitmap into an ArrayList / Stack, so everytime a new drawing happens, a new mBitmap is pushed into the array list / stack. Then, if someone wants to undo the last drawn stroke, I just need to pop the most recent Bitmap and load the 2nd most recent bitmap.</p>
  <p>I'm trying to implement FloodFill to fill drawings after this, so I think switching to this approach will be better long-run.</p>