Android View.onDraw() 总是有一个干净的 Canvas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2423327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android View.onDraw() always has a clean Canvas
提问by CaseyB
I am trying to draw an animation. To do so I have extended View and overridden the onDraw() method. What I would expect is that each time onDraw() is called the canvas would be in the state that I left it in and I could choose to clear it or just draw over parts of it (This is how it worked when I used a SurfaceView) but each time the canvas comes back already cleared. Is there a way that I can not have it cleared? Or maybe save the previous state into a Bitmap so I can just draw that Bitmap and then draw over top of it?
我正在尝试绘制动画。为此,我扩展了 View 并覆盖了 onDraw() 方法。我所期望的是,每次调用 onDraw() 时,画布都会处于我离开它的状态,我可以选择清除它或只绘制它的一部分(这就是我使用 SurfaceView 时的工作方式) 但每次画布回来时都已经清除了。有没有办法不清除它?或者也许可以将以前的状态保存到位图,这样我就可以绘制该位图,然后在它上面绘制?
回答by dweebo
I'm not sure if there is a way or not. But for my custom views I either redraw everything each time onDraw() is called, or draw to a bitmap and then draw the bitmap to the canvas (like you suggested in your question).
我不确定是否有办法。但是对于我的自定义视图,我要么在每次调用 onDraw() 时重新绘制所有内容,要么绘制到位图然后将位图绘制到画布上(就像您在问题中建议的那样)。
Here is how i do it
这是我如何做到的
class A extends View {
private Canvas canvas;
private Bitmap bitmap;
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (bitmap != null) {
bitmap .recycle();
}
canvas= new Canvas();
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
}
public void destroy() {
if (bitmap != null) {
bitmap.recycle();
}
}
public void onDraw(Canvas c) {
//draw onto the canvas if needed (maybe only the parts of animation that changed)
canvas.drawRect(0,0,10,10,paint);
//draw the bitmap to the real canvas c
c.drawBitmap(bitmap,
new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),
new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()), null);
}
}
回答by Sephy
you should have a look hereto see the difference between basic view and surfaceView. A surfaceView has a dedicated layer for drawing, which I suppose keeps track of what you drew before. Now if you really want to do it on a basic View, you could try to put each item you draw in an array, like the exemple of itemized overlay for the mapview. It should work pretty much the same way
你应该看看这里,看看基本视图和表面视图之间的区别。SurfaceView 有一个专门的绘图层,我想它会跟踪您之前绘制的内容。现在,如果您真的想在基本 View 上执行此操作,您可以尝试将您绘制的每个项目放入一个数组中,例如mapview的逐项叠加示例。它应该以几乎相同的方式工作
回答by Matt Shirilla
Your expectations do not jib w/ reality :) The canvas will not be the way you left it, but it blank instead. You could create an ArrayList of objects to be drawn (canvas.drawCircle(), canvas.drawBitmap() etc.), then iterate though the ArrayList in the OnDraw(). I am new to graphics programming but I have used this on a small scale. Maybe there is a much better way.
您的期望与现实不符 :) 画布不会像您留下的那样,而是空白。您可以创建要绘制的对象的 ArrayList(canvas.drawCircle()、canvas.drawBitmap() 等),然后遍历 OnDraw() 中的 ArrayList。我是图形编程的新手,但我已经小规模地使用过它。也许有更好的方法。