android - 如何撤消/重做上次触摸操作

标签 android undo-redo

我有一张正在处理的图像,我有两个按钮,撤消和重做。如果单击这两个按钮中的任何一个,我需要代码来撤消/重做之前的触摸操作。我知道我必须使用堆栈。我该如何实现?

最佳答案

实现撤消/重做主要有两种模式:

  1. “纪念品”模式。
  2. “命令”模式。

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 。每个“ 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/

相关文章:

Android默认浏览器不滚动网页

java - 在 JTextPane 中撤消和重做忽略样式更改

java - Java 中可撤消的文本区域

objective-c - 核心数据: How to merge inserts/updates/deletes between two NSManagedObjectContext's while maintaining the merge as an undoable step?

VIM::撤消指定文本上或周围的更改

javascript - 对可拖动(拖放)的元素应用撤消重做

java - Android登录无法连接DB

c# - Xamarin-遍历Android设备上的所有音频文件

android - GCM 无法启动服务 Intent

android - 如何从 Android 应用程序中的 twitter API 获取用户详细信息