From 7cffdf2d738dc205e9daae34b7da937999f0b98f Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Fri, 5 Apr 2024 14:09:29 -0700 Subject: [PATCH] Kotlinify LayoutUpdateAnimation (#43890) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43890 Changelog: [Internal] As part of the Sustainability Week (see [post](https://fb.workplace.com/groups/251759413609061/permalink/742797531171911/)). Reviewed By: cortinico Differential Revision: D55766128 fbshipit-source-id: a7f5fc653bd44ec7fc9f4995e82193d8b0d28660 --- .../LayoutUpdateAnimation.java | 49 ------------------- .../layoutanimation/LayoutUpdateAnimation.kt | 49 +++++++++++++++++++ 2 files changed, 49 insertions(+), 49 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.java deleted file mode 100644 index 2802c063a22..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.java +++ /dev/null @@ -1,49 +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.View; -import android.view.animation.Animation; -import android.view.animation.TranslateAnimation; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; - -/** - * Class responsible for handling layout update animation, applied to view whenever a valid config - * was supplied for the layout animation of UPDATE type. - */ -/* package */ @Nullsafe(Nullsafe.Mode.LOCAL) -class LayoutUpdateAnimation extends AbstractLayoutAnimation { - - // We are currently not enabling translation GPU-accelerated animated, as it creates odd - // artifacts with native react scrollview. This needs to be investigated. - private static final boolean USE_TRANSLATE_ANIMATION = false; - - @Override - boolean isValid() { - return mDurationMs > 0; - } - - @Override - @Nullable - Animation createAnimationImpl(View view, int x, int y, int width, int height) { - boolean animateLocation = view.getX() != x || view.getY() != y; - boolean animateSize = view.getWidth() != width || view.getHeight() != height; - if (!animateLocation && !animateSize) { - return null; - } else if (animateLocation && !animateSize && USE_TRANSLATE_ANIMATION) { - // Use GPU-accelerated animation, however we loose the ability to resume interrupted - // animation where it was left off. We may be able to listen to animation interruption - // and set the layout manually in this case, so that next animation kicks off smoothly. - return new TranslateAnimation(view.getX() - x, 0, view.getY() - y, 0); - } else { - // Animation is sub-optimal for perf, but scale transformation can't be use in this case. - return new PositionAndSizeAnimation(view, x, y, width, height); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.kt new file mode 100644 index 00000000000..e6a26665f70 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.kt @@ -0,0 +1,49 @@ +/* + * 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.View +import android.view.animation.Animation +import android.view.animation.TranslateAnimation + +/** + * Class responsible for handling layout update animation, applied to view whenever a valid config + * was supplied for the layout animation of UPDATE type. + */ +internal class LayoutUpdateAnimation : AbstractLayoutAnimation() { + + override internal fun isValid(): Boolean = mDurationMs > 0 + + override internal fun createAnimationImpl( + view: View, + x: Int, + y: Int, + width: Int, + height: Int + ): Animation? { + val animateLocation = view.x.toInt() != x || view.y.toInt() != y + val animateSize = view.width != width || view.height != height + if (!animateLocation && !animateSize) { + return null + } else if (animateLocation && !animateSize && USE_TRANSLATE_ANIMATION) { + // Use GPU-accelerated animation, however we loose the ability to resume interrupted + // animation where it was left off. We may be able to listen to animation interruption + // and set the layout manually in this case, so that next animation kicks off smoothly. + return TranslateAnimation(view.x - x, 0f, view.y - y, 0f) + } else { + // Animation is sub-optimal for perf, but scale transformation can't be use in this case. + return PositionAndSizeAnimation(view, x, y, width, height) + } + } + + private companion object { + // We are currently not enabling translation GPU-accelerated animated, as it creates odd + // artifacts with native react scrollview. This needs to be investigated. + private const val USE_TRANSLATE_ANIMATION = false + } +}