Android canvas redraw with 30fps
Im currently trying to implement the simple bouncing ball (in 2d) on my android device (physics based on device rotation and multiple balls coming in at a later point).
I have created a CustomDrawView
class that extends View
with a onDraw
function that draws a circle in the centre of the screen. The math to get it moving correctly around is irrelevent, as my question is where i should call invalidate
on the CustomDrawView.
- Should i make a new thread to handle this based on time? as in
Thread.sleep(1000/30); view.invalidate
(for 30fps) - Should i 开发者_C百科make it run in the same thread based on some other method?
Ofcourse battery is a important factor aswell, which method is best for saving battery?
From what I have seen with Android views they aren't the best if you are planning on making anything with framerate is very important. They snowball very quickly and the message passing pipeline used draw them is often the slowest thing your program will do that frame. I would just move the rendering now into OpenGL. Using the SurfaceView you can still get setup very quickly (ok SurfaceView also isn't great but much better than all Views).
If I were to use views, I would use your first method and setup a timing thread, use it sort of a like a standard Game loop that executes an update then triggers a draw then waits to ensure a full 1/30th of a second has passed and loops.
精彩评论