Android Canvas SurfaceView Black Screen
Solution 1:
You have the usual problem: you're overriding onDraw()
, which draws on the View part. So you have a very expensive custom View that doesn't work right. If you let the View draw, it'll draw once, but you're not calling invalidate()
so it never happens again. My guess is you set a background color in your layout, making the View opaque, which is why you can't see what's being drawn on the Surface.
For Canvas rendering, it's often better to use a custom View, rather than a SurfaceView. I would recommend that approach.
If you really want to use a SurfaceView, change onDraw()
to something else (like doDraw()
), and call it from your render thread. Since you'll be drawing on the Surface part, rather than the View part, you don't need to subclass SurfaceView (and shouldn't, as making it an independent class will avoid common pitfalls, such as one you fell in). Make sure you understand how SurfaceView and Activity interact, as there are various ways to get into trouble.
You should also read up on the recommended way to structure a game loop.
Solution 2:
I just changed overriden function onDraw(Canvas canvas) to draw(Canvas canvas) and it works!
Post a Comment for "Android Canvas SurfaceView Black Screen"