gpt4 book ai didi

android - 使用 Parcelable 将绘图保存到磁盘

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:42 26 4
gpt4 key购买 nike

我正在使用此代码保存和恢复我的自定义手绘 View ,如何使用此代码将我的绘图以矢量格式(而非位图)保存到 sdcard 上的文件在下一个应用程序 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();
}

最佳答案

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

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

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

关于android - 使用 Parcelable 将绘图保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42062556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com