title: android: paths to become part of the bitmap when requested
body: <p>I am learning on canvas and bitmap with paths and currently working on a drawing app where user can draw paths freely on the extended view. 
  The app also allow allowing user to import bitmap as the background and draw on it. </p>
  
  <p><strong>Extending the View named DoodleView:</strong></p>
  
  <pre><code>   public DoodleView(Context context, AttributeSet attrs) 
     {
         super(context, attrs); // pass context to View's constructor
         this.context_new=context;
         setFocusable(true);
         setFocusableInTouchMode(true);
     } // end DoodleView constructor
  </code></pre>
  
  <p><strong>onDraw:</strong></p>
  
  <pre><code>   @Override
     protected void onDraw(Canvas canvas) 
     {
         canvas.drawBitmap(bitmap, 0, 0, null); 
         for (Path p : paths)
         {
             paintLine.setColor(colorsMap.get(p));
             canvas.drawPath(p, paintLine);          
         }       
         paintLine.setColor(selectedColor);
         canvas.drawPath(mPath, paintLine);   
  
         if (ConvertCanvasToBitmap == true) 
         {
             canvas.drawBitmap(bitmap, 0, 0, paintLine);
             ConvertCanvasToBitmap = false;
         }           
     } 
  </code></pre>
  
  <p><strong>FlipHorizontally:</strong></p>
  
  <pre><code>public void flipImageHorizontally()  
  {       
      ConvertCanvasToBitmap = true;
      invalidate();           
      Matrix flipHorizontalMatrix = new Matrix();
      flipHorizontalMatrix.setScale(-1,1);
      flipHorizontalMatrix.postTranslate(bitmap.getWidth(),0);
  
      Bitmap HorizontalFlipped = Bitmap.createBitmap
              (bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipHorizontalMatrix, true);
      bitmap = HorizontalFlipped;
      invalidate();       
  }
  </code></pre>
  
  <h2>Question:</h2>
  
  <p>My goal is that for the paths that are already drawn, when the user tries to flip the image, the paths drawn would also be flipped. (i.e. the paths become part of the image already, and user are disallows to undo the paths anymore). </p>
  
  <p>However, I have tested using the above codes, when the flip button is pressed, the bitmap background can be flipped, yet the drawn would disappear. And then when further drawn on it, the paths will appear again, but stay unflipped.</p>
  
  <p>In short, how to make the paths to become part of the bitmap when the flip button is pressed?</p>
  
  <p>Thanks!</p>
  
  <h2>Edit:</h2>
  
  <p>Based on the <a href="https://stackoverflow.com/questions/2817166/android-2-1-views-getdrawingcache-method-always-returns-null">Android 2.1 View&#39;s getDrawingCache() method always returns null</a>, I have modified the <code>onDraw</code> with the following code, but got </p>
  
  <pre><code>02-22 21:38:34.685: E/AndroidRuntime(18617): java.lang.NullPointerException
  02-22 21:38:34.685: E/AndroidRuntime(18617):    at android.graphics.Bitmap.createBitmap(Bitmap.java:455)
  02-22 21:38:34.685: E/AndroidRuntime(18617):    at com.pearmak.drawing.DoodleView.onDraw(DoodleView.java:148)
  </code></pre>
  
  <p><strong>Modified code:</strong></p>
  
  <pre><code>   @Override
     protected void onDraw(Canvas canvas) 
     {
         canvas.drawBitmap(bitmap, 0, 0, null); // draw the background screen
  
         for (Path p : paths)
         {
             paintLine.setColor(colorsMap.get(p));
             paintLine.setStrokeWidth(widthMap.get(p));
             canvas.drawPath(p, paintLine);          
         }       
         paintLine.setColor(selectedColor);
         paintLine.setStrokeWidth(selectedWidth);
         canvas.drawPath(mPath, paintLine);   
  
         if (ConvertCanvasToBitmap == true) 
         {
             //Method 1
  //         RelativeLayout page = (RelativeLayout) findViewById(R.id.doodleView);
  //         Bitmap screenshot = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Config.ARGB_8888);
  //         bitmap = screenshot;
  //         ConvertCanvasToBitmap = false;
  
             //Method 2
             Bitmap screenshot2;
                 layout(0, 0, DoodlzViewWidth, DoodlzViewHeight);
             setDrawingCacheEnabled(true);
             screenshot2 = Bitmap.createBitmap(getDrawingCache()); // LINE 148
             setDrawingCacheEnabled(false);
             bitmap = screenshot2;
         }           
     } 
  </code></pre>