Animation glitches while rendering on SurfaceView

2019-01-29 11:21发布

问题:

The FPS is around 60, but the animation still glitches sometimes. I did threads profiling and discovered that on Samsung S4 or Samsung S3 lockCanvas() could take up to 20ms sometimes. Is there any specific reason for it? How to avoid such glitches.

回答1:

Some devices, particularly those based around qcom CPUs, will aggressively manage power by lowering clocks, so you will see occasionally glitches in 60fps animation when your finger isn't actively moving across the touchscreen.

The best way to handle this is to drop frames when necessary. The "Record GL app" activity in Grafika uses a simple trick with Choreographer to do this. For a detailed explanation, see this appendix in the graphics architecture doc.

I should also point out that Canvas rendering on a SurfaceView surface isn't hardware-accelerated, and as pixel counts increase the rendering time will increase as well, making it more likely that you'll miss your window. In some ways the added CPU load can actually make things smoother -- if the power management believes the device is heavily loaded it won't lower the clocks -- but it does drain the battery faster.