Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slow animation mode on Android #6014

Merged
merged 9 commits into from
Jun 12, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.lang.Float.NaN;

import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.view.View;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.GuardedRunnable;
Expand Down Expand Up @@ -50,6 +51,10 @@

public class NodesManager implements EventDispatcherListener {

private Long mFirstUptime = SystemClock.uptimeMillis();
private boolean mSlowAnimationsEnabled = false;
private int mAnimationsDragFactor;

public void scrollTo(int viewTag, double x, double y, boolean animated) {
View view;
try {
Expand Down Expand Up @@ -268,6 +273,10 @@ private void onAnimationFrame(long frameTimeNanos) {
// Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "onAnimationFrame");

double currentFrameTimeMs = frameTimeNanos / 1000000.;
if (mSlowAnimationsEnabled) {
currentFrameTimeMs =
mFirstUptime + (currentFrameTimeMs - mFirstUptime) / mAnimationsDragFactor;
}
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved

if (currentFrameTimeMs > lastFrameTimeMs) {
// It is possible for ChoreographerCallback to be executed twice within the same frame
Expand Down Expand Up @@ -523,4 +532,12 @@ private static void addProp(WritableMap propMap, String key, Object value) {
throw new IllegalStateException("[Reanimated] Unknown type of animated value.");
}
}

public void enableSlowAnimations(boolean slowAnimationsEnabled, int animationsDragFactor) {
mSlowAnimationsEnabled = slowAnimationsEnabled;
mAnimationsDragFactor = animationsDragFactor;
if (slowAnimationsEnabled) {
mFirstUptime = SystemClock.uptimeMillis();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class NativeProxyCommon {
private KeyboardAnimationManager keyboardAnimationManager;
private Long firstUptime = SystemClock.uptimeMillis();
private boolean slowAnimationsEnabled = false;
private final int ANIMATIONS_DRAG_FACTOR = 10;
protected String cppVersion = null;

protected NativeProxyCommon(ReactApplicationContext context) {
Expand Down Expand Up @@ -78,6 +79,7 @@ private void toggleSlowAnimations() {
if (slowAnimationsEnabled) {
firstUptime = SystemClock.uptimeMillis();
}
mNodesManager.enableSlowAnimations(slowAnimationsEnabled, ANIMATIONS_DRAG_FACTOR);
}

private void addDevMenuOption() {
Expand Down Expand Up @@ -156,7 +158,6 @@ public void setGestureState(int handlerTag, int newState) {
@DoNotStrip
public long getAnimationTimestamp() {
if (slowAnimationsEnabled) {
final long ANIMATIONS_DRAG_FACTOR = 10;
return this.firstUptime
+ (SystemClock.uptimeMillis() - this.firstUptime) / ANIMATIONS_DRAG_FACTOR;
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-reanimated/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"collapsable",
"devs",
"easings",
"gesturehandler",
"inout",
"ispreview",
"layoutable",
Expand Down
Loading