我正在做的是绘图应用程序,该项目需要两个功能是撤消和重做..所以我需要在每次用户绘制并从屏幕上拉起手指后保存先前绘图的列表..
这是保存先前绘图时的代码
public void saveState() {
State mUndoState = new State();
saveState(mSurface.getBitmap(), mUndoState);
}
private void saveState(Bitmap bitmap, State state) {
state.mBuffer = new byte[bitmap.getRowBytes() * bitmap.getHeight()];
Buffer byteBuffer = ByteBuffer.wrap(state.mBuffer);
bitmap.copyPixelsToBuffer(byteBuffer);
mListUndoState.add(state);
System.out.println("Size now: " + (mListUndoState.size() - 1));
mListRedoState.clear();
mListRedoState.add(state);
// StylesFactory.saveState(state.stylesState);
}
private static class State {
byte[] mBuffer = null;
// final HashMap<Integer, Object> stylesState = new HashMap<Integer,
// Object>();
}
问题是 android 设备只有 16 MB 的堆内存.. 处理这个问题的最佳方法是什么?.. 我只能将它保存到 ListUndoState 7 到 10 次,然后我得到了内存不足异常 .. 我想获得无限的撤消操作或至少不少于 50 次。
这是保存先前绘图以进行撤消和重做的完整类。
package com.appshouse.drawgram.utli;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;
public class HistoryHelper {
private final Surface mSurface;
private List<State> mListUndoState = new ArrayList<State>();
private List<State> mListRedoState = new ArrayList<State>();
public HistoryHelper(Surface surface) {
mSurface = surface;
}
public void undo() {
int length = mListUndoState.size() - 1;
if (length <= 0) {
System.out.println("no element is list");
return;
}
System.out.println("history undo size: " + length);
restoreState(mSurface.getBitmap(), mListUndoState.get(length - 1));
mListRedoState.add(mListUndoState.get(length));
mListUndoState.remove(length);
}
public void redo() {
int length = mListRedoState.size() - 1;
if (length <= 0) {
System.out.println("no element is list");
return;
}
System.out.println("history undo size: " + length);
restoreState(mSurface.getBitmap(), mListRedoState.get(length));
mListUndoState.add(mListRedoState.get(length));
mListRedoState.remove(length);
}
private void restoreState(Bitmap bitmap, State state) {
Buffer byteBuffer = ByteBuffer.wrap(state.mBuffer);
bitmap.copyPixelsFromBuffer(byteBuffer);
// StylesFactory.restoreState(state.stylesState);
}
public void saveState() {
State mUndoState = new State();
saveState(mSurface.getBitmap(), mUndoState);
}
private void saveState(Bitmap bitmap, State state) {
state.mBuffer = new byte[bitmap.getRowBytes() * bitmap.getHeight()];
Buffer byteBuffer = ByteBuffer.wrap(state.mBuffer);
bitmap.copyPixelsToBuffer(byteBuffer);
mListUndoState.add(state);
System.out.println("Size now: " + (mListUndoState.size() - 1));
mListRedoState.clear();
mListRedoState.add(state);
// StylesFactory.saveState(state.stylesState);
}
private static class State {
byte[] mBuffer = null;
// final HashMap<Integer, Object> stylesState = new HashMap<Integer,
// Object>();
}
}