forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate DecayAnimation to Kotlin (facebook#45719)
Summary: Pull Request resolved: facebook#45719 # Changelog: [Internal] - As in the title. Reviewed By: tdn120 Differential Revision: D60284323
- Loading branch information
1 parent
a5ec7c7
commit 0ec513d
Showing
2 changed files
with
71 additions
and
79 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
...s/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...ges/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.animated | ||
|
||
import com.facebook.react.bridge.ReadableMap | ||
import kotlin.math.abs | ||
import kotlin.math.exp | ||
|
||
/** | ||
* Implementation of [AnimationDriver] providing support for decay animations. The implementation is | ||
* copied from the JS version in `AnimatedImplementation.js`. | ||
*/ | ||
internal class DecayAnimation(config: ReadableMap) : AnimationDriver() { | ||
private var velocity: Double = 0.0 | ||
private var deceleration: Double = 0.0 | ||
private var startFrameTimeMillis: Long = -1 | ||
private var fromValue: Double = 0.0 | ||
private var lastValue: Double = 0.0 | ||
private var iterations: Int = 1 | ||
private var currentLoop: Int = 1 | ||
|
||
init { | ||
resetConfig(config) | ||
} | ||
|
||
public override fun resetConfig(config: ReadableMap): Unit { | ||
velocity = config.getDouble("velocity") | ||
deceleration = config.getDouble("deceleration") | ||
startFrameTimeMillis = -1 | ||
fromValue = 0.0 | ||
lastValue = 0.0 | ||
iterations = if (config.hasKey("iterations")) config.getInt("iterations") else 1 | ||
currentLoop = 1 | ||
mHasFinished = iterations == 0 | ||
} | ||
|
||
public override fun runAnimationStep(frameTimeNanos: Long) { | ||
val frameTimeMillis = frameTimeNanos / 1000000 | ||
if (startFrameTimeMillis == -1L) { | ||
// since this is the first animation step, consider the start to be on the previous frame | ||
startFrameTimeMillis = frameTimeMillis - 16 | ||
if (fromValue == lastValue) { // first iteration, assign mFromValue based on mAnimatedValue | ||
fromValue = mAnimatedValue.nodeValue | ||
} else { // not the first iteration, reset mAnimatedValue based on mFromValue | ||
mAnimatedValue.nodeValue = fromValue | ||
} | ||
lastValue = mAnimatedValue.nodeValue | ||
} | ||
val value = | ||
(fromValue + | ||
velocity / (1 - deceleration) * | ||
(1 - exp(-(1 - deceleration) * (frameTimeMillis - startFrameTimeMillis)))) | ||
if (abs(lastValue - value) < 0.1) { | ||
if (iterations == -1 || currentLoop < iterations) { // looping animation, return to start | ||
// set mStartFrameTimeMillis to -1 to reset instance variables on the next runAnimationStep | ||
startFrameTimeMillis = -1 | ||
currentLoop++ | ||
} else { // animation has completed | ||
mHasFinished = true | ||
return | ||
} | ||
} | ||
lastValue = value | ||
mAnimatedValue.nodeValue = value | ||
} | ||
} |