0
votes

My program is a simple flood-fill game application for android, namely, the user can paint and draw something on the canvas. Now I want to provide a sharing option to the users. I guess that I must begin with copying the canvas to a bitmap object. I could not find a satisfactory answer because it is generally suggested "to create a new canvas, then..." but I got a canvas like that,

Canvas canvas = holder.lockCanvas();

then I use it. So, how can I copy my current canvas to a bitmap object?

Thanks

1
When i use Bitmap abc = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); Canvas canvas = holder.lockCanvas(); canvas.setBitmap(abc); Then, it is just black screen.adba
I guess that I must Why guess? Why not read the documentation?Simon
I read them. There are a few options that I have tried and failed. I guess that the right way is to copy the canvas to a bitmap object. But I could not succeed. Then, I asked it here. Isn't it proper method for asking a question?adba

1 Answers

2
votes

As suggested Maulik.J, I looked at converting a canvas into bitmap image in android again.

I could not understand from a bit close expression of this link. But, then I saw below text taken from http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas . So, I solved this problem with the help of Maulik.J and the following text:

The Bitmap is always required for a Canvas. You can set up a new Canvas like this:

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);

Now your Canvas will draw onto the defined Bitmap. After drawing upon it with the Canvas, you can then carry your Bitmap to another Canvas with one of the Canvas.drawBitmap(Bitmap,...) methods. It's recommended that you ultimately draw your final graphics through a Canvas offered to you by View.onDraw() or SurfaceHolder.lockCanvas()

I created a second Canvas object having a bitmap which has same dimensions with my real canvas, I drew all my illustrations on this second temporary canvas. Then, I drew its bitmap on my real canvas.

Thanks.