xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.java (#45739)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45739

Changelog: [Internal]

Reviewed By: tdn120

Differential Revision: D60233508

fbshipit-source-id: fcf666cb5d2a935c85b0b4507739d002b58f8141
This commit is contained in:
Andrew Datsenko
2024-07-30 05:40:59 -07:00
committed by Facebook GitHub Bot
parent 4d647be848
commit 995d4b4c36
2 changed files with 48 additions and 50 deletions
@@ -1,50 +0,0 @@
/*
* 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.uimanager.layoutanimation;
import android.view.animation.Interpolator;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
/** Simple spring interpolator */
// TODO(7613736): Improve spring interpolator with friction and damping variable support
/* package */ @Nullsafe(Nullsafe.Mode.LOCAL)
class SimpleSpringInterpolator implements Interpolator {
private static final float FACTOR = 0.5f;
public static final String PARAM_SPRING_DAMPING = "springDamping";
private final float mSpringDamping;
public static float getSpringDamping(ReadableMap params) {
if (params.getType(PARAM_SPRING_DAMPING).equals(ReadableType.Number)) {
return (float) params.getDouble(PARAM_SPRING_DAMPING);
} else {
return FACTOR;
}
}
public SimpleSpringInterpolator() {
mSpringDamping = FACTOR;
}
public SimpleSpringInterpolator(float springDamping) {
mSpringDamping = springDamping;
}
@Override
public float getInterpolation(float input) {
// Using mSpringDamping in this equation is not really the exact mathematical springDamping, but
// a good approximation
// We need to replace this equation with the right Factor that accounts for damping and friction
return (float)
(1
+ Math.pow(2, -10 * input)
* Math.sin((input - mSpringDamping / 4) * Math.PI * 2 / mSpringDamping));
}
}
@@ -0,0 +1,48 @@
/*
* 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.uimanager.layoutanimation
import android.view.animation.Interpolator
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.facebook.react.bridge.ReadableType.Number
/** Simple spring interpolator */
// TODO(7613736): Improve spring interpolator with friction and damping variable support
/* package */
internal class SimpleSpringInterpolator : Interpolator {
private val _springDamping: Float
@JvmOverloads
public constructor(springDamping: Float = FACTOR) {
_springDamping = springDamping
}
override public fun getInterpolation(input: Float): Float =
// Using mSpringDamping in this equation is not really the exact mathematical springDamping,
// but a good approximation
// We need to replace this equation with the right Factor that accounts for damping and
// friction
(1 +
Math.pow(2.0, (-10 * input).toDouble()) *
Math.sin((input - _springDamping / 4) * Math.PI * 2 / _springDamping))
.toFloat()
public companion object {
private const val FACTOR = 0.5f
public const val PARAM_SPRING_DAMPING: String = "springDamping"
@JvmStatic
public fun getSpringDamping(params: ReadableMap): Float =
if (params.getType(PARAM_SPRING_DAMPING) == ReadableType.Number) {
params.getDouble(PARAM_SPRING_DAMPING).toFloat()
} else {
FACTOR
}
}
}