我正在尝试画动画。为此,我扩展了 View 并重写了 onDraw() 方法。我期望的是,每次调用 onDraw() 时,画布都会处于我保留它的状态,我可以选择清除它或只绘制它的一部分(这就是我使用 SurfaceView 时的工作方式) )但每次画布回来时都已经清除了。有什么办法可以让我不清除它吗?或者也许将以前的状态保存到位图中,这样我就可以绘制该位图,然后在它上面绘制?
我不确定是否有办法。但对于我的自定义视图,我要么在每次调用 onDraw() 时重新绘制所有内容,要么绘制位图,然后将位图绘制到画布上(就像您在问题中建议的那样)。
我是这样做的
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)