我想将画布的当前状态保存为图像,因为我将在下一个指针事件中使用它。如果我使用重绘,它将清除画布,我无法获得画布的先前状态。所以,我想将它保存为图像,然后一遍又一遍地迭代它,以便最终我可以找出我想要的东西。最后一个问题是如何将画布保存为图像?或者是否有可能将 Graphics 对象转换为字节数组?
问问题
323 次
2 回答
1
您无法将画布另存为图像。
您必须先创建图像,然后才能在该图像上绘画。
基本上这意味着您的 midlet 将做更多的工作,因为您首先必须在图像上绘制,然后必须将该图像绘制到画布上。但这是你可以做你想做的事情的唯一方法。
于 2013-04-20T08:41:01.250 回答
1
创建一个Image
具有相同大小(宽度和高度)的屏幕。当您要保存画布状态时调用Canvas.paint
传递图像Graphics
。
class MyCanvas extends Canvas {
private Image lastScreen;
protected void sizeChanged(int w, int h) {
if (lastScreen == null || w != lastScreen.getWidth()
|| h != lastScreen.getHeight) {
lastScreen = Image.createImage(w, h);
}
}
protected void paint(Graphics g) {
// paint the whole screen
}
protected void pointerReleased(int x, int y) {
paint(lastScreen.getGraphics());
}
}
于 2013-04-22T11:22:45.797 回答