【发布时间】:2015-03-01 13:04:26
【问题描述】:
我正在开发一个绘画应用程序,其中在 doodleView 中绘制的路径及其各自的颜色和宽度存储如下:
变量
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();
private Map<Path, Integer> widthMap = new HashMap<Path, Integer>();
private Map<Path, Integer> styleMap = new HashMap<Path, Integer>();
运动事件
private void touch_start(float x, float y)
{
undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y)
{
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
{
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
startdrawing = true;
}
}
private void touch_up()
{
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, Utilities.mPaint);
paths.add(mPath);
colorsMap.put(mPath,selectedColor);
widthMap.put(mPath,selectedWidth);
styleMap.put(mPath, selectedStyle);
mPath = new Path();
}
public void onClickUndo()
{
if (paths.size()>0)
{
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
else
{
String xx = getContext().getString(R.string.toast_nothing_to_undo);
Activity activity = (Activity) context_new;
Utilities.custom_toast(activity, ""+ xx, "gone!", "Short");
}
}
public void onClickRedo()
{
if (undonePaths.size()>0)
{
paths.add(undonePaths.remove(undonePaths.size()-1));
invalidate();
}
else
{
String xx = getContext().getString(R.string.toast_nothing_to_redo);
Activity activity = (Activity) context_new;
Utilities.custom_toast(activity, ""+ xx, "gone!", "Short");
}
}
问题:
以上代码运行良好。但是,从主页按钮/通知栏返回,有时路径会从内存中释放。
我想问一下如何保存路径以便可以在创建时重绘路径?可以保存到 SharedPreference 吗?
【问题讨论】:
标签: android canvas path sharedpreferences