Android在绘图应用程序中实现撤消堆栈

发布于 2024-11-25 22:17:00 字数 998 浏览 7 评论 0原文

我开始开发 Android SDK 中的示例 Finger Paint 应用程序,以更加熟悉图形。最近,我一直在尝试实现撤消/重做,但我尝试过的各种方法都遇到了障碍。我发现了一些与此相关的主题,但没有一个主题能让我解决这些问题。以下是我的主要 2 个试验:

策略 1:

保存一堆路径(或画布),并在撤消时清除屏幕并重新绘制除最后一条路径之外的每条路径(或恢复最近的画布)。

这里的问题可能很简单,但我就是无法获得重绘任何内容的视图。如何绘制保存的路径(或恢复保存的画布)?

策略 2:

每次触摸后使用 getDrawingCache() 保存一堆位图。撤消时,将最后一个位图放回原处。

保存必须通过可运行的 post() 运行,因此它在 onDraw() 完成后执行(post 将其添加到 invalidate() 之后的系统消息行)。问题是,当从可运行的 getDrawingCache() 运行时,它总是返回绘画的初始版本,就像它在第一次之后看不到任何更改一样。

为什么从可运行对象调用 getDrawingCache() 时看不到视图的当前状态?

我已经为此奋斗了一段时间。谢谢。

case MotionEvent.ACTION_UP:
touch_up();
invalidate();
Runnable r = new Runnable(){
    @Override
    public void run() {
    myView.storeView();
    }
};
myView.post(r);
}

public void storeView(){
    historyCount++;
    if(historyCount > historySize) historyCount = 6;    //We don't want more than 6                        
    history.add(Bitmap.createBitmap(myView.getDrawingCache()),historyCount);
}

I started working on the sample Finger Paint app in the Android SDK to get more familiar with graphics. Lately I've been trying to implement undo/redo, and I've ran into road blocks every way I've tried. I've found a few threads about this, but none have gotten me past these issues. Here are my main 2 trials:

Strategy 1:

Save a stack of the paths (or canvases) and on undo clear the screen and redraw each path except the last one (or reinstate the most recent canvas).

The problem here is likely simple, but I just can't get the view to redraw anything. How do I draw saved paths (or restore a saved canvas)?

Strategy 2:

Save a stack of Bitmaps using getDrawingCache() after each touch. On undo, put the last bitmap back.

The saving has to be ran via post() from a runnable so it executes after onDraw() finishes (post adds it to the system message line after invalidate()). The issue is that when ran from the runnable getDrawingCache() always returns the initial version of the painting, like it can't see any changes after the first.

Why does getDrawingCache(), when called from a runnable, not see the current state of the view?

I've been fighting with this a while. Thanks.

case MotionEvent.ACTION_UP:
touch_up();
invalidate();
Runnable r = new Runnable(){
    @Override
    public void run() {
    myView.storeView();
    }
};
myView.post(r);
}

public void storeView(){
    historyCount++;
    if(historyCount > historySize) historyCount = 6;    //We don't want more than 6                        
    history.add(Bitmap.createBitmap(myView.getDrawingCache()),historyCount);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-12-02 22:17:00

对于策略 1,您需要定义一个数据结构来表示渲染绘图的一部分所需的所有信息。因此,举例来说,如果您通过跟踪用户的触摸位置来绘制绘图的一部分,请定义一个数据结构,其中包含当前颜色、绘图形状以及由一个用户手势生成的坐标的 ArrayList。当您跟随用户的触摸、绘制到屏幕时,还将触摸坐标附加到当前手势的 ArrayList 中。当触摸结束时,将数据结构推送到撤消堆栈上,并等待下一个用户手势来创建下一个结构实例并开始填充它。

如果您有多种手势(填充、徒手描绘、直线等),则可以为每种手势设置单独的结构。它们都可以从抽象类继承,因此它们都可以进入堆栈。

第二种策略在我看来是一种可怕的内存消耗。我认为我们必须查看您的代码才能理解为什么它没有按预期工作。

For strategy 1, you need to define a data structure that represents all the information needed to render a part of the drawing. So, for instance, if you draw a segment of the drawing by tracking the user's touch position, define a data structure that consists of the current color, drawing shape, and an ArrayList of coordinates used generated by one user gesture. As you follow the user's touch, drawing to the screen, also append the touch coordinates to the ArrayList for the current gesture. When the touch ends, push the data structure onto the undo stack and wait for the next user gesture to create the next structure instance and start populating it.

If you have various gestures (fill, freehand trace, straight line, etc.), you can have a separate structure for each. They can all inherit from an abstract class so they can all go on the stack.

The second strategy strikes me as a horrible memory hog. I think we'd have to see your code to understand why it isn't working as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文