TransitionY 动画不与 TransitionManager 一起播放

TranslationY animation doesn't play with TransitionManager

我对动画有疑问。我定义了两个简单的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/player1"
        android:layout_width="50dp"
        android:layout_height="77dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:src="@drawable/two_hearts"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/player1"
        android:layout_width="50dp"
        android:layout_height="77dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:src="@drawable/two_hearts"
        android:translationY="-60dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

长相:

他们的区别是android:translationY="-60dp".

我在GameFragment.kt中切换它们:

class GameFragment: Fragment() {

    private lateinit var root: ConstraintLayout
    private var expanded = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        root = inflater.inflate(R.layout.fragment_game_hidden, container, false) as ConstraintLayout
        root.setOnClickListener {
            show()
        }
        return root
    }

    private fun show() {
        if (expanded) {
            updateConstraints(R.layout.fragment_game_hidden)
        } else {
            updateConstraints(R.layout.fragment_game_shown)
        }
        expanded = !expanded
    }

    private fun updateConstraints(@LayoutRes id: Int) {
        val newConstraintSet = ConstraintSet()
        newConstraintSet.clone(context, id)
        val transition = ChangeBounds()
        transition.interpolator = AccelerateDecelerateInterpolator()
        transition.duration = 1000
        newConstraintSet.applyTo(root)
        TransitionManager.beginDelayedTransition(root, transition)
    }

}

我的问题是,根本没有播放动画,布局上的卡片只是跳转到目标位置,没有任何过渡。为什么?我觉得我在这里遗漏了一些明显的东西......

删除 android:translationY="-60dp" 中的:fragment_game_hidden.xml。直接用margin就可以了

尝试更改为以下代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/player1"
        android:layout_width="50dp"
        android:layout_height="77dp"
        android:layout_marginStart="32dp"
        android:src="@drawable/two_hearts"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>