我正在为android用户开发一款绘图应用程序。我想在我的应用程序上实现撤消重做功能。我引用stackoverflow上发布的问题来解决我的问题,但我无法为我的代码找到正确的解决方案。下面我发布了我的代码,请帮助我解决这个问题。如有任何帮助,我们将不胜感激。
提前谢谢。
public class DrawingView extends View {
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
setupDrawing();
}
public void setupDrawing(){
drawPath = new Path();
drawPaint = new Paint();
canvasPaint = new Paint(Paint.DITHER_FLAG);
brushSize = getResources().getInteger(R.integer.medium_size);
lastBrushSize = brushSize;
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(brushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
paths.add(drawPath);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//view given size
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
//draw view
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
for(Path p : paths){
canvas.drawPath(p, drawPaint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// undonePaths.clear();
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
// drawPath.lineTo(touchX, touchY);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
drawPath = new Path();
paths.add(drawPath);
break;
default:
return false;
}
invalidate();
return true;
}
public void onClickUndo(){
if(paths.size()>0){
undonePaths.add(paths.remove(paths.size() - 1));
Toast.makeText(getContext(), "Undo is working", Toast.LENGTH_LONG).show();
invalidate();
}
else{
Toast.makeText(getContext(), "Undo is not working", Toast.LENGTH_LONG).show();
}
}
public void onClickRedo(){
if(undonePaths.size()>0){
paths.add(undonePaths.remove(undonePaths.size() - 1));
Toast.makeText(getContext(), "Redo is working", Toast.LENGTH_LONG).show();
invalidate();
}
else{
Toast.makeText(getContext(), "Redo is not working", Toast.LENGTH_LONG).show();
}
}
}
只是一些小改动。
在您的列表中有一个额外的Path
,它将导致"撤消/重做"步骤在其余部分得到纠正时基本上什么都不做。所以在setupDrawing()
中删除:
paths.add(drawPath);
在onDraw()
方法中,删除:
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
此drawBitmap()
调用正在重新绘制在每个ACTION_UP
上绘制到的成员位图,使其看起来好像您的Path
列表没有更新一样。
然后在onTouchEvent()
方法中,在MotionEvent.ACTION_DOWN
中取消注释:
undonePaths.clear();
并将MotionEvent.ACTION_UP
的情况更改为:
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
paths.add(drawPath);
drawPath = new Path();
break;
reset()
调用正在清除您不想要的Path
。相反,在这里,我们将最近完成的Path
添加到列表中,然后创建一个新的来继续。
注意:按照目前的工作方式,撤消/恢复功能不会影响canvasBitmap
。由于不清楚这个位图是用来做什么的,所以我保持原样。如果你想让这个位图镜像视图的,你可以简单地将drawCanvas.drawPath()
调用移动到onDraw()
,与那里的canvas.drawPath()
调用并行。
var undoRedo = new Array();
var unStep = -1;
function historySave() {
unStep++;
while (undoRedo.length > 20) {
undoRedo.shift();
unStep--;
}
if (unStep !== 0 && unStep < undoRedo.length) {
undoRedo.length = unStep;
unStep++;
} else {
undoRedo.length = unStep;
}
undoRedo.push(document.getElementById('drawingCanvas').toDataURL());
}
function Undo() {
if (unStep > -1) {
unStep--;
var canvasPic = new Image();
canvasPic.src = undoRedo[unStep];
drawingContext.clearRect(0,0,drawingCanvas.width,drawingCanvas.height);
canvasPic.onload = function () {
drawingContext.drawImage(canvasPic, 0, 0);
}
}
}
function Redo() {
if (unStep < undoRedo.length - 1) {
unStep++;
var canvasPic = new Image();
canvasPic.src = undoRedo[unStep];
canvasPic.onload = function () {
drawingContext.drawImage(canvasPic, 0, 0);
}
}
}