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?
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?
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
Try this code :
Canvas canvas = ...;
Bitmap bitmap = Bitmap.createBitmap(canvas);
canvas.setBitmap(bitmap);