Reputation: 19184
I am using this code to save and restore my custom hand drawing view, how can I use this code to save my drawings in vector format, not bitmap, to a file on sdcard and reload it in next app session:
@Override
protected Parcelable onSaveInstanceState() {
// Get the superclass parcelable state
Parcelable superState = super.onSaveInstanceState();
if (mPoints.size() > 0) {// Currently doing a line, save it's current path
createHistoryPathFromPoints();
}
return new FreeDrawSavedState(superState, mPaths, mCanceledPaths,
mCurrentPaint, mPaintColor, mPaintAlpha, mResizeBehaviour,
mLastDimensionW, mLastDimensionH);
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
// If not instance of my state, let the superclass handle it
if (!(state instanceof FreeDrawSavedState)) {
super.onRestoreInstanceState(state);
return;
}
FreeDrawSavedState savedState = (FreeDrawSavedState) state;
// Superclass restore state
super.onRestoreInstanceState(savedState.getSuperState());
// My state restore
mPaths = savedState.getPaths();
mCanceledPaths = savedState.getCanceledPaths();
mCurrentPaint = savedState.getCurrentPaint();
initFillPaint();
mResizeBehaviour = savedState.getResizeBehaviour();
mPaintColor = savedState.getPaintColor();
mPaintAlpha = savedState.getPaintAlpha();
// Restore the last dimensions, so that in onSizeChanged i can calculate the
// height and width change factor and multiply every point x or y to it, so that if the
// View is resized, it adapt automatically it's points to the new width/height
mLastDimensionW = savedState.getLastDimensionW();
mLastDimensionH = savedState.getLastDimensionH();
notifyRedoUndoCountChanged();
}
Upvotes: 4
Views: 570
Reputation: 6200
I did this in one of my application. I created shapes using free hand drawing. Elements that I used were Group, Layer, Path, Point, Shape
and their properties. I have created the json array from Path
or Layer
and some of their properties like strokeWidth, dashEffect, color, etc.
. Stored all these in json format in table. It worked fine on one device, but as soon as you send the json to server and draw the same on other mobile device the drawing was either drawn small or big in shape. It was because of the device resolution or density
factor that I didn't took in to account. Before sending to the server I calculated or fetched the density factor for the point
used in Path
on the very first drawing of the image. Then I had send this density
factor to the server. Whenever I drew this drawing on other mobile, I used this density
factor to in drawing calculation.
I would not recommend to use Parcelable
as it is used to communicate between components.
That extra efficiency has in turn its limits: passing an image ( Bitmap) using Parcelable is generally not a good idea (although Bitmap does in fact implement Parcelable). A much more memory-efficient way would be to pass only its URI or Resource ID, so that other Android components in your application can have access to it.
Another limitation of Parcelable is that it must not be used for general-purpose serialization to storage, since the underlying implementation may vary with different versions of the Android OS. So yes, Parcels are faster by design, but as high-performance transport, not as a replacement for general-purpose serialization mechanism.
Took the above block quote from: must not be used for general-purpose serialization to storage
Upvotes: 3
Reputation: 1089
as of my understanding you want to save your custom drawing image in svg/vector file format. for this try this library available on github. compile 'com.github.gcacace:signature-pad:1.2.1'
by using this library you can save bitmap to svg file and parse as string in Parcelable
Upvotes: 1
Reputation: 3383
You need to create your own file format for storing your custom vector drawing.
Saving all your points and their properties into a JSON files is a possible solution.
Android's Parcelable
is not meant for persistent storage, just for in-app or inter process communication. Because the binary format can change between Android updates for whatever reasons e.g. smaller memory or cpu footprint of the new binary format. Thus loading after such update will lead to loss of data.
Upvotes: 5
Reputation: 1045
I think you should draw all of your drawing on a bitmap and then save bitmap on sdcard in onSaveInstanceState()
method.you can find code for saving bitmap in here
Android saving Bitmap to SD card
you should check if you saved bitmap already if yes you should reload it and if no you should draw it.
Upvotes: -1