|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager.layoutanimation |
| 9 | + |
| 10 | +import android.view.View |
| 11 | +import android.view.animation.Animation |
| 12 | +import android.view.animation.ScaleAnimation |
| 13 | +import com.facebook.react.common.annotations.internal.LegacyArchitecture |
| 14 | +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel |
| 15 | +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger |
| 16 | +import com.facebook.react.uimanager.IllegalViewOperationException |
| 17 | + |
| 18 | +/** Class responsible for default layout animation, i.e animation of view creation and deletion. */ |
| 19 | +@LegacyArchitecture |
| 20 | +internal abstract class BaseLayoutAnimation : AbstractLayoutAnimation() { |
| 21 | + abstract fun isReverse(): Boolean |
| 22 | + |
| 23 | + override fun isValid(): Boolean = mDurationMs > 0 && mAnimatedProperty != null |
| 24 | + |
| 25 | + override fun createAnimationImpl(view: View, x: Int, y: Int, width: Int, height: Int): Animation { |
| 26 | + mAnimatedProperty?.let { |
| 27 | + return when (it) { |
| 28 | + AnimatedPropertyType.OPACITY -> { |
| 29 | + val fromValue = if (isReverse()) view.alpha else 0.0f |
| 30 | + val toValue = if (isReverse()) 0.0f else view.alpha |
| 31 | + OpacityAnimation(view, fromValue, toValue) |
| 32 | + } |
| 33 | + |
| 34 | + AnimatedPropertyType.SCALE_XY -> { |
| 35 | + val fromValue = if (isReverse()) 1.0f else 0.0f |
| 36 | + val toValue = if (isReverse()) 0.0f else 1.0f |
| 37 | + ScaleAnimation( |
| 38 | + fromValue, |
| 39 | + toValue, |
| 40 | + fromValue, |
| 41 | + toValue, |
| 42 | + Animation.RELATIVE_TO_SELF, |
| 43 | + .5f, |
| 44 | + Animation.RELATIVE_TO_SELF, |
| 45 | + .5f) |
| 46 | + } |
| 47 | + |
| 48 | + AnimatedPropertyType.SCALE_X -> { |
| 49 | + val fromValue = if (isReverse()) 1.0f else 0.0f |
| 50 | + val toValue = if (isReverse()) 0.0f else 1.0f |
| 51 | + ScaleAnimation( |
| 52 | + fromValue, |
| 53 | + toValue, |
| 54 | + 1f, |
| 55 | + 1f, |
| 56 | + Animation.RELATIVE_TO_SELF, |
| 57 | + .5f, |
| 58 | + Animation.RELATIVE_TO_SELF, |
| 59 | + 0f) |
| 60 | + } |
| 61 | + |
| 62 | + AnimatedPropertyType.SCALE_Y -> { |
| 63 | + val fromValue = if (isReverse()) 1.0f else 0.0f |
| 64 | + val toValue = if (isReverse()) 0.0f else 1.0f |
| 65 | + ScaleAnimation( |
| 66 | + 1f, |
| 67 | + 1f, |
| 68 | + fromValue, |
| 69 | + toValue, |
| 70 | + Animation.RELATIVE_TO_SELF, |
| 71 | + 0f, |
| 72 | + Animation.RELATIVE_TO_SELF, |
| 73 | + .5f) |
| 74 | + } |
| 75 | + } |
| 76 | + } ?: throw IllegalViewOperationException("Missing animated property from animation config") |
| 77 | + } |
| 78 | + |
| 79 | + private companion object { |
| 80 | + init { |
| 81 | + LegacyArchitectureLogger.assertWhenLegacyArchitectureMinifyingEnabled( |
| 82 | + "BaseLayoutAnimation", LegacyArchitectureLogLevel.WARNING) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments