0

I am using a CanvasView API I found online and I am wondering how to save the drawings made on the screen for when I change to and from activities.

This is the code that handles the canvas, it's pretty simple. However, whenever I go to and from a page, the drawings disappear. Does anyone know how I could save the drawings?

public CanvasView canvas;

public void initializeCanvas(View v, int color, float strokeWidth) {
    canvas = (CanvasView)v.findViewById(R.id.canvas);
    canvas.setMode(CanvasView.Mode.DRAW);
    canvas.setDrawer(CanvasView.Drawer.PEN);
    canvas.setPaintStyle(Paint.Style.STROKE);
    canvas.setOpacity(200);
    canvas.setPaintStrokeColor(color);
    canvas.setPaintStrokeWidth(strokeWidth);
}

https://github.com/Korilakkuma/CanvasView

Brejuro
  • 3,421
  • 8
  • 34
  • 61

1 Answers1

0

I couldn't find anything in there to save everything in entirety. But then again I'm not good at reading APIs.

You want to save the activity instance before pausing it (moving to another activity). Android does some of it by default but not too much. You need to specify the details (much like you make a better copy-constructor for deep copy).

This article on Recreating Activity can give you the basic idea. Now you have several options on what and how you save in savedInstanceState and restore later:

Everything below is untested. Try the first one, if it doesn't work, go crazy. Let the future readers know what worked.

  • CanvasView.java has a convenient public Bitmap getBitmap() method. Get the bitmap image from there. Save the bitmap temporarily somehow. On restore, use the public void drawBitmap(Bitmap bitmap) method to restore drawing. You will lose drawing history and settings of stuff and things. To save them too, you can try stuff below.
  • Save some select attributes of the Canvas classes using the provided accessor methods (e.g. public int getBaseColor()) and restore them later using the public modifiers (e.g. public void setBaseColor(int color))
  • Modify the classes from API to make motherOfGetters(...) and motherOfSetters(.....) methods (I made the names up). They will provide you values of most important data types to save and later restore them using the setter.
  • Save your API objects in savedInstanceState. This SO post on saving objects may help with that.
Community
  • 1
  • 1
Noobification
  • 319
  • 1
  • 14