Reputation: 1716
From what I've seen on SO, what I want to do is difficult if even possible, but I hope that someone can at least suggest a less roundabout way of doing what I want. The code below draws items onto a canvas, all within a custom SurfaceView. What I want to do is, in addition to displaying the canvas, save everything on it to a file for use in a couple of activities, but I can't seem to figure out how.
public void draw()
{
if(holder.getSurface().isValid()) {
Canvas canvas = holder.lockCanvas();
canvas.drawColor(0, Mode.CLEAR);
drawSurface(canvas);
canvas.translate(-56, 30);
int i = 0;
for(Picture a: accessories) {
canvas.translate(0, a_offset[i]);
if(a!=null) {
canvas.drawPicture(a);
}
i++;
}
holder.unlockCanvasAndPost(canvas);
}
}
private void drawSurface(Canvas canvas) {
Rect rect = new Rect();
rect.set(37*screenWidth/480, 146*screenHeight/800, (37 + 300)*screenWidth/480, (146 + 300)*screenHeight/800);
canvas.drawBitmap(avatar, null, rect, null);
}
What I'm currently doing to save the file is essentially using copies of the methods above with a Canvas created from a bitmap (canvas = new Canvas(bitmap)
instead of holder.lockCanvas
) and with different numbers to get the positions and sizes correct. I could combine the methods and give them some parameters at least to avoid having different methods, but I'd like to just draw the images and save the file using the same canvas, if possible.
Upvotes: 3
Views: 712
Reputation: 9182
You shouldn't be storing the canvas. You should be storing the image transformation matrix and the image itself. Then if you want to retrieve, load the image, load its matrix, apply the matrix to the image and you should be fine.
Or you could try storing the canvas object itself as blob data and then try to recreate it from the database using the blob. I've never done this though and wouldn't recommend it in any case.
Upvotes: 1