首页 > 解决方案 > ConstraintLayout ConstraintSet 立即应用而不是动画

问题描述

我有以下布局:

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

    <!-- ... -->

    <View
        android:id="@+id/fieldView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="4dp"
        android:elevation="6dp"
        app:layout_constraintTop_toBottomOf="@id/topLayout" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/actionsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="parent"
        tools:layout_constraintTop_toBottomOf="@id/fieldView">

    <!-- ... -->

我想actionsLayout从屏幕底部(实际上是从屏幕外)滑动到fieldView.

    ConstraintSet()
            .apply {
                clone(context, R.layout.fragment_problem)

                connect(R.id.actionsLayout, ConstraintSet.TOP, R.id.fieldView, ConstraintSet.BOTTOM)
            }
            .runAnimation(rootLayout, actionsLayoutAppearAnimationDuration, OvershootInterpolator())

runAnimation资源:

fun ConstraintSet.runAnimation(rootLayout: ConstraintLayout, duration: Long, interpolator: Interpolator = LinearInterpolator()) {
    val transition = AutoTransition()
        .apply {
            this.duration = duration
            this.interpolator = interpolator
        }

    TransitionManager.beginDelayedTransition(rootLayout, transition)
    this.applyTo(rootLayout)
}

注意:此扩展功能适用于其他布局。

但是在这个布局和这个动画上,它不会动画约束改变,而是立即应用它。所以,布局看起来和我预期的一样,除了动画——根本没有动画。为什么?

标签: androidandroid-animationandroid-constraintlayout

解决方案


看起来开始动画onViewCreated并不是最好的主意。最简单的解决方案是在rootLayout.post().


推荐阅读