Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8a9328f

Browse files
axe-fbcampsafari
authored andcommitted
Add support for springDamping in SpringInterpolator
Reviewed By: mdvacca Differential Revision: D7201334 fbshipit-source-id: 50929b4294188cd5a2a8ffa2080c38c0a9983535
1 parent 2e38418 commit 8a9328f

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/AbstractLayoutAnimation.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
package com.facebook.react.uimanager.layoutanimation;
44

5-
import javax.annotation.Nullable;
6-
7-
import java.util.Map;
8-
5+
import android.os.Build;
96
import android.view.View;
107
import android.view.animation.AccelerateDecelerateInterpolator;
118
import android.view.animation.AccelerateInterpolator;
129
import android.view.animation.Animation;
10+
import android.view.animation.BaseInterpolator;
1311
import android.view.animation.DecelerateInterpolator;
1412
import android.view.animation.Interpolator;
1513
import android.view.animation.LinearInterpolator;
16-
1714
import com.facebook.react.bridge.ReadableMap;
1815
import com.facebook.react.common.MapBuilder;
1916
import com.facebook.react.uimanager.IllegalViewOperationException;
17+
import java.util.Map;
18+
import javax.annotation.Nullable;
2019

2120
/**
2221
* Class responsible for parsing and converting layout animation data into native {@link Animation}
@@ -36,12 +35,11 @@
3635
*/
3736
abstract @Nullable Animation createAnimationImpl(View view, int x, int y, int width, int height);
3837

39-
private static final Map<InterpolatorType, Interpolator> INTERPOLATOR = MapBuilder.of(
38+
private static final Map<InterpolatorType, BaseInterpolator> INTERPOLATOR = MapBuilder.of(
4039
InterpolatorType.LINEAR, new LinearInterpolator(),
4140
InterpolatorType.EASE_IN, new AccelerateInterpolator(),
4241
InterpolatorType.EASE_OUT, new DecelerateInterpolator(),
43-
InterpolatorType.EASE_IN_EASE_OUT, new AccelerateDecelerateInterpolator(),
44-
InterpolatorType.SPRING, new SimpleSpringInterpolator());
42+
InterpolatorType.EASE_IN_EASE_OUT, new AccelerateDecelerateInterpolator());
4543

4644
private @Nullable Interpolator mInterpolator;
4745
private int mDelayMs;
@@ -64,7 +62,7 @@ public void initializeFromConfig(ReadableMap data, int globalDuration) {
6462
if (!data.hasKey("type")) {
6563
throw new IllegalArgumentException("Missing interpolation type.");
6664
}
67-
mInterpolator = getInterpolator(InterpolatorType.fromString(data.getString("type")));
65+
mInterpolator = getInterpolator(InterpolatorType.fromString(data.getString("type")), data);
6866

6967
if (!isValid()) {
7068
throw new IllegalViewOperationException("Invalid layout animation : " + data);
@@ -100,8 +98,16 @@ public void initializeFromConfig(ReadableMap data, int globalDuration) {
10098
return animation;
10199
}
102100

103-
private static Interpolator getInterpolator(InterpolatorType type) {
104-
Interpolator interpolator = INTERPOLATOR.get(type);
101+
private static Interpolator getInterpolator(InterpolatorType type, ReadableMap params) {
102+
Interpolator interpolator = null;
103+
if (type.equals(InterpolatorType.SPRING)) {
104+
interpolator = new SimpleSpringInterpolator(SimpleSpringInterpolator.getSpringDamping(params));
105+
} else {
106+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
107+
// Conversion from BaseInterpolator to Interpolator is only possible on this Android versions
108+
interpolator = INTERPOLATOR.get(type);
109+
}
110+
}
105111
if (interpolator == null) {
106112
throw new IllegalArgumentException("Missing interpolator for type : " + type);
107113
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package com.facebook.react.uimanager.layoutanimation;
44

55
import android.view.animation.Interpolator;
6+
import com.facebook.react.bridge.ReadableMap;
7+
import com.facebook.react.bridge.ReadableType;
68

79
/**
810
* Simple spring interpolator
@@ -11,10 +13,30 @@
1113
/* package */ class SimpleSpringInterpolator implements Interpolator {
1214

1315
private static final float FACTOR = 0.5f;
16+
public static final String PARAM_SPRING_DAMPING = "springDamping";
17+
private final float mSpringDamping;
18+
19+
public static float getSpringDamping(ReadableMap params){
20+
if (params.getType(PARAM_SPRING_DAMPING).equals(ReadableType.Number)){
21+
return (float) params.getDouble(PARAM_SPRING_DAMPING);
22+
} else {
23+
return FACTOR;
24+
}
25+
}
26+
27+
public SimpleSpringInterpolator() {
28+
mSpringDamping = FACTOR;
29+
}
30+
31+
public SimpleSpringInterpolator(float springDamping){
32+
mSpringDamping = springDamping;
33+
}
1434

1535
@Override
1636
public float getInterpolation(float input) {
37+
// Using mSpringDamping in this equation is not really the exact mathematical springDamping, but a good approximation
38+
// We need to replace this equation with the right Factor that accounts for damping and friction
1739
return (float)
18-
(1 + Math.pow(2, -10 * input) * Math.sin((input - FACTOR / 4) * Math.PI * 2 / FACTOR));
40+
(1 + Math.pow(2, -10 * input) * Math.sin((input - mSpringDamping / 4) * Math.PI * 2 / mSpringDamping));
1941
}
2042
}

0 commit comments

Comments
 (0)