使用 Parcelable 将绘图保存到磁盘

AVE*_*imi 4 android instance android-custom-view android-activity

我正在使用此代码来保存和恢复我的自定义手绘视图,我如何使用此代码将我的绘图以矢量格式而不是位图保存到SD 卡上的文件并在下一个应用程序会话中重新加载它:

@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();
}
Run Code Online (Sandbox Code Playgroud)

she*_*lll 5

您需要创建自己的文件格式来存储自定义矢量绘图。

将所有点及其属性保存到 JSON 文件中是一种可能的解决方案。

AndroidParcelable不是用于持久存储,而是用于应用内或进程间通信。因为无论出于何种原因,例如较小的内存或新二进制格式的 CPU 占用空间,二进制格式都可能在 Android 更新之间发生变化。因此在此类更新后加载将导致数据丢失。