Nino van Hooff
Nino van Hooff

Reputation: 3893

Does every view have its own canvas/bitmap to draw on?

I have a relativelayout with three full screen child views of the same custom view class. I am wondering whether I should be worried about memory. Judging by this answer: Understanding Canvas and Surface concepts

All views get passed the same canvas with underlying bitmap to draw on so that the memory is not tripled. Can anyone confirm? It would make sense, otherwise a full screen textview would be very inefficient.

Bonus: is the purpose of canvas to define a drawable area of a bitmap and translating the view coordinates to bitmap coordinates?

Upvotes: 2

Views: 451

Answers (1)

Henry
Henry

Reputation: 17851

As per the documentation http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas:

When you're writing an application in which you would like to perform specialized drawing and/or control the animation of graphics, you should do so by drawing through a Canvas. A Canvas works for you as a pretense, or interface, to the actual surface upon which your graphics will be drawn — it holds all of your "draw" calls. Via the Canvas, your drawing is actually performed upon an underlying Bitmap, which is placed into the window.

In the onDraw(Canvas canvas), you are given a canvas object. This canvas has an underlying bitmap. All views are not given the same canvas. Canvas is just a layer above the common bitmap (which is pixels on the screen). canvas offers you to manipulate the bitmap as much as you want. So every view has a canvas, but not it's own bitmap.

So no, as far as memory is concerned, three view doesn't mean memory is tripled, because there is just one bitmap. You could however create your own bitmap, if you do so, then you will be jogging up the memory. If you create 3 bitmaps with size of the screen, your memory will be tripled.

Upvotes: 2

Related Questions