Android SurfaceView onDraw 问题
我的 SurfaceView 的 onDraw 方法绘制了由屏幕上的 TouchEvents 确定的路径的 LinkedList。我想添加一个撤消功能,从 LinkedList 中删除最后一个路径节点。当按下按钮时,路径将从列表中删除,但 SurfaceView 不会更新,直到我再次按下撤消按钮或触摸屏幕。
谢谢, 埃里克
My SurfaceView's onDraw method draw's a LinkedList of paths determined by TouchEvents on the screen. I want add an undo feature that removes the last path node from the LinkedList. When the button is hit, the path is removed from the list but the SurfaceView doesn't update until I either hit the undo button again or touch the screen.
Thanks,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该使用:
不是
postInvalidate();
告诉主 UI 线程在下一个方便的时间重绘。您这样做的方式不适用于单独的线程。you should be using:
not
postInvalidate();
tells the main UI thread to redraw on its next convenient time. The way you are doing it will not work on a seperate thread.这是因为视图仅在调用 onDraw() 时更新,这是由于屏幕活动(例如您触摸它)或以编程方式强制这样做时发生的。
您想强制它:使用从 View 继承的 invalidate() 方法。
如果删除发生在 SurfaceView 内部,只需在删除完成后编写 invalidate() 即可。如果它发生在外部,则只需执行 surfaceViewInstance.invalidate() 即可。
希望这有帮助
That's because Views are only updated when onDraw() is called, which happens due to screen activity (like you touching it) or when it's programatically forced to do so.
You want to force it: use the invalidate() method inherited from View.
If the removal happens inside you SurfaceView, just write invalidate() AFTER you're done removing. If it happens outside, then just do surfaceViewInstance.invalidate().
Hope this helps