Android timing in OpenGL ES thread is not monotonic

前端 未结 1 1653
庸人自扰
庸人自扰 2020-12-18 16:49

I\'m measuring time interval for looped animations/particles/etc in my Android app. App is a live wallpaper so it doesn\'t prevent system from scaling clock down for power s

相关标签:
1条回答
  • 2020-12-18 17:19

    Expanding a little on what I think you're doing based on the outline of your code, your onDrawFrame() method in pseudo code looks like this:

    deltaTime = SystemClock.elapsedRealtime() - lastFrameTrime
    // point 1, see text below
    update animation based on deltaTime
    draw frame
    // point 2, see text below
    lastFrameTime = SystemClock.elapsedRealtime()
    

    The problem with this sequence is that you lose the time between point 1 and point 2 from your total animation time. You need to make sure that the total sum of all your deltaTime values, which are the times applied to your animation, covers the entire wall clock time of the amimation. With the logic you use, this is not the case. You only add up the times between the end of one call to the start of the next call. You do not account for the time needed to execute the method, which can be significant.

    This can be fixed with a slight change in the sequence:

    currentTime = SystemClock.elapsedRealtime()
    deltaTime = currentTime - lastFrameTime
    lastFrameTime = currentTime
    update animation based on deltaTime
    draw frame
    

    The key point is that you only call elapsedRealtime() once per frame.

    0 讨论(0)
提交回复
热议问题