从旧画布中绘制 - Android
我正在制作一个应用程序,需要能够在最后一组图形的基础上绘制新图形。
这是我当前的 onDraw() 方法 -
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
if(points.size() > 0) {
//do some stuff here - this is all working ok
canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
}
}
基本上,我需要将新图形绘制为最后一个图形之上的图层,因此我正在寻找一种将最后一个画布的图像带到当前画布的方法。
我尝试使用 canvas.setBitmap() 方法自己解决这个问题,但它的行为非常有趣。
任何帮助表示赞赏:)
PS如果需要的话,该类扩展 SurfaceView
并实现 SurfaceHolder.Callback
编辑:这是我在 onDraw() 方法中尝试过的,但它只是强制关闭
if(bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.setBitmap(bitmap);
}
I'm making an App that needs to be able to draw new graphics on top of the last set.
This is my current onDraw() method -
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
if(points.size() > 0) {
//do some stuff here - this is all working ok
canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
}
}
Basically, I need to draw the new graphics as a layer on top of the last, so what I'm looking for is a way to carry the image of the last canvas to the current.
I have tried to figure it out myself using the canvas.setBitmap() method but it acts very funny.
Any help appreciated :)
P.S if it's needed, the the class extends SurfaceView
and implements SurfaceHolder.Callback
Edit: This is what I have tried in the onDraw() method but it just force closes
if(bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.setBitmap(bitmap);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己找到了答案:)
完全按照预期工作,它创造了在旧画布上连续绘图的效果
Found the answer myself :)
Works exactly as intended, it creates the effect of drawing on top of the old canvas continuously
您必须将前一个图像持久存储到 ArrayList 中,并在 ondraw 期间循环遍历 ArrayList 以重绘所有项目。
像这样的东西:
You will have to store the previous image persistently onto a ArrayList, and during ondraw, loop through the ArrayList to redraw all items.
something like this: