1
我試圖在我的應用程序中實現撤消和重做。我正在閱讀其他問題和論壇,但代碼不適合我。我只在這裏發佈相關的代碼,以便於閱讀,因爲我繪製了不同的形狀。對於重做和撤消,我在另一個活動中有兩個按鈕,我在每個按鈕的onclicklistener中調用函數redo或undo。撤消和重做畫布
private void setupDrawing(){
drawPath=new Path();
//Set up paint
paths.add(drawPaint);
}
public boolean onTouchEvent(MotionEvent event){
tX=event.getX();
tY=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//Action move down
break;
case MotionEvent.ACTION_MOVE:
//Action move
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawCircle(x, y, radius, drawPaint); //Example of drawing object
drawPath = new Path();
paths.add(drawPath);
break;
default:
return false;
}
invalidate();
return true;
}
public void undo(){
if(paths.size()>0){
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
if(undo==true){
undo=false;
}else{
undo=true;
}
}
public void redo(){
if(undonePaths.size()>0){
paths.add(undonePaths.remove(undonePaths.size()-1));
invalidate();
}
if(redo==true){
redo=false;
}else{
redo=true;
}
}