Saving a drawing to disk using Parcelable

I use this code to save and restore my custom drawing view manually, how can I use this code to save my drawings in vector format, rather than a bitmap, in a file on sdcard and reload it into the following application session:

@Override
protected Parcelable onSaveInstanceState() {

    // Get the superclass parcelable state
    Parcelable superState = super.onSaveInstanceState();

    if (mPoints.size() > 0) {// Currently doing a line, save it 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 points to the new width/height
    mLastDimensionW = savedState.getLastDimensionW();
    mLastDimensionH = savedState.getLastDimensionH();

    notifyRedoUndoCountChanged();
}
+4
source share
4 answers

You need to create your own file format for storing a custom vector drawing.

Saving all of your points and their properties in JSON files is a possible solution.

Android Parcelable , . Android - , . . , .

+4

. . , , Group, Layer, Path, Point, Shape . json Path Layer , strokeWidth, dashEffect, color, etc.. json . , json , , . density, . point, Path, . density . , , density .

Parcelable, .

, , : (Bitmap) Parcelable ( Bitmap Parcelable). URI , Android .

Parcelable , , Android. , , , .

:

+3

, svg/vector. , github.   compile 'com.github.gcacace: -pad: 1.2.1'

, bitmap svg Parcelable

+1

, , sdcard onSaveInstanceState().

Android SD-

, , , , , .

-1

Source: https://habr.com/ru/post/1668907/


All Articles