Ziva
Ziva

Reputation: 3501

Saving changes on surfaceView each invalidate()

I have a class which extends SurfaceView and implements SurfaceHolder.Callback. Using onDraw() method i'm drawing some bitmaps on my canvas. The, after pressing a button I'm adding new image to the canvas calling invalidate(). Is any possibility to save all the changes which I made on every invalidate() which were earlier, so as to after new invalidate() add new image, but not delete the earlier?

Upvotes: 0

Views: 637

Answers (3)

Nyashka
Nyashka

Reputation: 1

You can save your old drawings on canvas and dont draw them again when adding new picture. Just call invalidate(x, y, x+sizeX, y+sizeY) when you need to add new image with left-top point's coordinates (x, y) and size (sizeX, sizeY) to the View's canvas. About to save all images in array you already got answer. P.S. sorry for my english, hope it was helpfull

Upvotes: 0

Michael Dotson
Michael Dotson

Reputation: 529

This is pretty straightforward case of using Bitmaps to store your previous canvas. Simply attach a bitmap to your canvas. Before drawing, save the old bitmap to something like "prevBitmap" and then draw over it. You can redraw the previous Bitmap by calling canvas.drawBitmap(prevBitmap);

Upvotes: 0

Simon Zettervall
Simon Zettervall

Reputation: 1784

From what I have understood you will want to save the image that was drawn and also it's position? By using the code I provided you will have a list that is filled with the image and position. The list is unordered; if you would like an ordered list you can use a LinkedList instead.

Create a new class, you may name it anything.

public class ImageHolder {
    private int mX;
    private int mY;
    private int mDrawableResource;
    private String mBitmapFilePath;

    public ImageHolder(int x, int y, int drawableResource, String bitmapFilePath) {
        mX = x;
        mY = y;
        mDrawableResource = drawableResource;
        mBitmapFilePath = bitmapFilePath;
    }

    public int getX() {
        return mX;
    }

    public int getY() {
        return mY;
    }

    public int getDrawableResource() {
        return mDrawableResource;
    }

    public String getBitmapFilePath() {
        return mBitmapFilePath;
    }
}

Then in your SurfaceView you add it a holder each time you draw to a list. Notice that this is bare minimum code so no synchronization has been added.

private void customDrawMethod() {
        mImageHolders.add(new ImageHolder(x, y, drawableResource, bitmapFilePath));

        Canvas canvas = getHolder().lockCanvas();

        canvas.drawBitmap(bitmap, matrix, paint);

        getHolder().unlockCanvasAndPost(canvas);
    }

I added both a Bitmap and a Drawable because I am unsure what you use. I could not post more code because I have no idea what you want to do with the saved images. More info could help you further.

Upvotes: 1

Related Questions