我有一个正在处理的图像,我有两个按钮,撤消和重做。如果单击这两个按钮中的任何一个,我需要代码来撤消/重做之前的触摸操作。我知道我必须使用堆栈。我应该如何实现它?
最佳答案
实现撤消/重做有两种主要模式:
1. Memento Pattern
备忘录模式的想法是你可以保存一个对象的整个内部状态的副本(不违反封装),以便稍后恢复。
它会像这样使用(例如):
// Create your object that can be "undone"
ImageObject myImage = new ImageObject()
// Save an "undo" point.
var memento = myImage.CreateMemento();
// do a bunch of crazy stuff to the image...
// ...
// Restore to a previous state.
myImage.SetMemento(memento);
2. Command Pattern
命令模式的思想是封装实际在对象上执行的操作。每个“ Action ”(或“命令”)都可以选择知道如何回滚自身。或者,当需要发生回滚时,可以再次执行整个命令链。
它会像这样使用(例如):
// Create your object that can be "undone"
ImageObject myImage = new ImageObject()
// Create a "select all" command
var command = new SelectAllCommand(myImage); // This does not actually execute the action.
// Apply the "select all" command to the image
selectAll.Execute(); // In this example, the selectAll command would "take note" of the selection that it is overwriting.
// When needed, rollback:
selectAll.Rollback(); // This would have the effect of restoring the previous selection.
关于android - 如何撤消/重做上次触摸操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5156848/