Ivan Seidel
Ivan Seidel

Reputation: 2372

How to draw Canvas on Canvas

I`m building an android App, and i got stuck with a simple thing: How do i draw (or "add") a Canvas object, to another Canvas object, like "merging" them?

If that`s not possible, what is the best solution for doing that?

Thanks!

Upvotes: 1

Views: 10205

Answers (1)

Cat
Cat

Reputation: 67522

This depends entirely on your implementation.

  • If each Canvas draws objects directly from an array (of shapes, etc.) each frame, you could simply append one array to the other. This way, your Canvas does not need to be drastically altered, it only has to add one array to another (possibly an ArrayList would be the way to go here).

  • If the above is not the case, you may have to make some more drastic changes. When I encountered a similar problem, I created a new method called commitChanges(), which added a series of changes to an existing Canvas (adding lines on top, etc.).

    1. I first invalidated the affected area, then created a Bitmap with the size of the Canvas: Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_8888);.
    2. Next, I created a canvas from that Bitmap: Canvas workingDrawing = new Canvas(bmp);.
    3. Then, I drew everything I needed onto that new Canvas. In this case, that would be the data from one of your Canvases.
    4. Now, in your other Canvas, you have to get the Bitmap you just drew, then draw it onto this Canvas. Like so: canvas.drawBitmap(yourDrawnBitmap, 0.0f, 0.0f, null);.

I think the difficulty you'll face is transferring the data from one Canvas to another. But, regardless of your implementation, one of the above methods should work effectively for you.

Upvotes: 9

Related Questions