Skip to content

Commit

Permalink
Fix rounding resulting in choppy timing animations
Browse files Browse the repository at this point in the history
Summary: Casting to long too early here and dropping some precision, resulting in skipped (not dropped) frames.

Reviewed By: sahrens

Differential Revision: D3819153

fbshipit-source-id: 83676cf4c9129638348890c74d563db121049e4a
  • Loading branch information
foghina authored and Facebook Github Bot 3 committed Sep 7, 2016
1 parent de3457f commit 40baf6a
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
class FrameBasedAnimationDriver extends AnimationDriver {

// 60FPS
private static final double FRAME_TIME_MILLIS = 1000d / 60d;

private long mStartFrameTimeNanos = -1;
private final double[] mFrames;
private final double mToValue;
Expand All @@ -40,10 +43,8 @@ public void runAnimationStep(long frameTimeNanos) {
mStartFrameTimeNanos = frameTimeNanos;
mFromValue = mAnimatedValue.mValue;
}
long timeFromStartNanos = (frameTimeNanos - mStartFrameTimeNanos);
// frames are calculated at 60FPS, to get index by a given time offset from the start of the
// animation, we take the time diff in millisecond and divide it by 60 frames per 1000ms.
int frameIndex = (int) (timeFromStartNanos / 1000000L * 60L / 1000L);
long timeFromStartMillis = (frameTimeNanos - mStartFrameTimeNanos) / 1000000;
int frameIndex = (int) (timeFromStartMillis / FRAME_TIME_MILLIS);
if (frameIndex < 0) {
throw new IllegalStateException("Calculated frame index should never be lower than 0");
} else if (mHasFinished) {
Expand Down

0 comments on commit 40baf6a

Please sign in to comment.