1

I have a canvas object and i'm searching for a method to draw a canvas into a Bitmap object like that:

Canvas canvas = ...;
Bitmap bitmap = Bitmap.createBitmap(canvas);

Is there a way to do that?

Filippo
  • 274
  • 1
  • 4
  • 13

2 Answers2

0

This may help:

private Bitmap getBitmap(int drawableRes, Context context) {
    Drawable drawable = context.getResources().getDrawable(drawableRes);
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 
    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 
    drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}

You call it like this:

     Bitmap drawableBitmap = getBitmap(YourResource, mContext);

So you create a new Bitmap, with width and height being the same as your canvas.

  Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )
  canvas.setBitmap(myBitmap)

but not drawBitmap

Roga Lu
  • 162
  • 1
  • 3
  • 14
-2

Try this code :

Canvas canvas = ...;
Bitmap bitmap = Bitmap.createBitmap(canvas);

canvas.setBitmap(bitmap);
riciprini
  • 103
  • 4