【问题标题】:ConstraintLayout ConstraintSet applies instantly instead of animationConstraintLayout ConstraintSet 立即应用而不是动画
【发布时间】:2020-01-04 05:53:18
【问题描述】:

我有以下布局:

<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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2017-11-15
      • 2021-01-29
      • 2016-04-03
      • 1970-01-01
      • 2020-11-08
      • 2018-08-18
      相关资源
      最近更新 更多