0

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.

Extending the View named DoodleView:

   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

onDraw:

   @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;
       }           
   } 

FlipHorizontally:

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();       
}

Question:

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).

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.

In short, how to make the paths to become part of the bitmap when the flip button is pressed?

Thanks!

Edit:

Based on the Android 2.1 View's getDrawingCache() method always returns null, I have modified the onDraw with the following code, but got

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)

Modified 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;
       }           
   } 
Community
  • 1
  • 1
pearmak
  • 4,979
  • 15
  • 64
  • 122

1 Answers1

0

you need to create the bitmap from that view please refer to drawingcache like here

Android 2.1 View's getDrawingCache() method always returns null

now try to flip this bitmap

Community
  • 1
  • 1
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • (as added in the above question) i have followed the suggested link and tried to get the screenshot according to the suggested solution. Yet I got NullPointerException and further work with the suggested link to add layout(0,0,x,y), yet still fails to capture the drawn paths together with the bitmap. Do you have some clue for it? many thanks! – pearmak Feb 22 '13 at 13:46
  • I am extending View from the Main Activity. The extended View is named DoodleView. If the layout in the onDraw part need to be get referenced to a view, how could it be coded? I really dont know ... – pearmak Feb 22 '13 at 13:59