Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top