How can I store canvas drawings in draw(Canvas)
method of a SurfaceView
so that at the next draw(Canvas)
callback I can draw from a saved image/canvas/bitmap instead of redrawing all the stuff?
Asked
Active
Viewed 528 times
0

bluePhlavio
- 537
- 5
- 18
-
Possible duplicate of [Android SurfaceView not retaining previously drawn objects](http://stackoverflow.com/questions/23622162/android-surfaceview-not-retaining-previously-drawn-objects) – Doug Stevenson Mar 28 '16 at 08:48
-
@Doug Stevenson It seem different from what I asked... I asked what is the best way to store the canvas status in order to redraw from that in the future. Bitmap? Canvas copy? whatever... – bluePhlavio Mar 28 '16 at 09:17
-
Canvas doesn't really have a "status". It contains a series of drawing commands. If you're asked to draw in onDraw, then just repeat the series of draw commands that creates the display you want at any given time. Android will make sure that series of draws is reflected to the view. Unless you're try to optimize an obvious performance problem, just do this. – Doug Stevenson Mar 28 '16 at 09:27
-
@Doug Stevenson Yes, I'm trying to optimize because at every step I have to draw thousand points evolving paths on the surfaceview. So the idea was to keep the drawing in a bitmap and draw at every step only the new parts of the paths... – bluePhlavio Mar 28 '16 at 09:31
-
2If you need to retain the state of the previous draw, save it to a different Canvas that draws to a Bitmap, then draw that Bitmap to the new Canvas during onDraw if appropriate. – Doug Stevenson Mar 28 '16 at 09:38
-
@Doug Stevenson Ok, is this the best way? I don't know how canvas work but if I create a bitmap of the size of the surfaceview all drawings out of the view will be loss. Is a drawing outside the view rectangle stored in the canvas or not? The problem is that canvas matrix can change so that part of the paths that were outside the view can became visible but with your method these drawings aren't accessible anymore because they're outside the bitmap... – bluePhlavio Mar 28 '16 at 09:49
-
Don't think of the Canvas as "storing" anything. Think of it as a way to express a series of draw commands that end up at some destination. A bitmap could be that destination, and you are free to redraw that bitmap on another canvas later if you want. Don't worry about optimizing until you have an obvious performance problem. – Doug Stevenson Mar 28 '16 at 09:53
-
@Doug Stevenson Ok, I understand. But I want to optimize because I need it! The drawing of thousand points paths 25 times per second seems to be heavy. I thought this was a tyipical situation to optimize... Isn't it? – bluePhlavio Mar 28 '16 at 09:57
-
You don't need it until you have benchmarks that prove you need it. But it's your call. – Doug Stevenson Mar 28 '16 at 10:03
-
The paths can grow in lenght indefinitely. So soon or later some mechanism of optimization is needed. Also, I see performance get lower after few time... – bluePhlavio Mar 28 '16 at 10:07
-
Indefinite draws sounds like a whole different problem that you didn't address in the question. How does anyone address the performance problems surrounding "infinity"? – Doug Stevenson Mar 28 '16 at 10:09
-
Looks like this is continued in http://stackoverflow.com/questions/36258875/how-to-save-canvas-from-surfaceview – fadden Mar 28 '16 at 20:28