ConstraintLayout ConstraintSet立即应用而不是动画

问题描述 投票:0回答:1

我有以下布局:

<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)
}

注意:此扩展功能在其他布局上也可以正常工作。

但是在此布局和此动画上,它不会为约束变化设置动画,而只是立即应用它。因此,除动画外,布局看起来与我期望的一样—根本没有动画。为什么?

android android-animation android-constraintlayout
1个回答
0
投票

[看起来onViewCreated中的动画不是最好的主意。最简单的解决方案是在rootLayout.post()中调用它。

© www.soinside.com 2019 - 2024. All rights reserved.