Animation ist only smooth when ontouchevent is cal

2019-06-14 04:35发布

问题:

i'm making my first 2d sidescroller game using a surfaceview and a canvas to draw things on (a lot of primitives put in different path objects). my game loop uses fixed timesteps with linear interpolation. i don't create any objects during the game. i've been improving my code for 3 weeks now, but my animation is still not all the time smooth. it's ok, but every few seconds there are a lot of little hicks for about 1 or 2 seconds. what i recognized is when i move my player (this means touching the screen), the little hicks disappear for as long as i touch the screen and move my player. this means as long as ontouchevent of the surfaceview is called, the animation is smooth. i dont understand this and i want a smooth animation. can somebody help me?

回答1:

This sounds like a known issue on certain devices. See e.g.:

  • Android SurfaceView slows if no touch events occur
  • Animation glitches while rendering on SurfaceView
  • Android thread performance/priority on Galaxy Note 2 when screen is touched/released

The problem is that the system is aggressively reducing clock speeds to save power when it doesn't detect interaction with the user. (Qualcomm in particular seems fond of this.) The workaround is to drop frames when necessary. See this article on game loops, and a Choreographer-based trick demonstrated in Grafika's "record GL app" activity (in doFrame()).