Android SurfaceView canvas drawing with a thread

时间秒杀一切 提交于 2019-12-03 14:36:27

it seems the app is switching between two canvasses

Yes, this is how it works. It is called double buffering and you need to redraw all the frame each time:

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written.

So you need this line canvas.drawColor(Color.BLACK) to be uncommented in your code.

And you shouldn't call Thread.sleep(1000) while canvas is locked, it will cause starvation issue.

It sounds like you have this working, but I did just notice a small error that I should point out.

You called canvas.restore() without calling canvas.save() beforehand. From the Android developer reference for Canvas: "It is an error to call restore() more times than save() was called."

I don't see any reason for you to call canvas.save() in your case, therefore you should remove the call to canvas.restore().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!