我有以下布局:
<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)
}
注意:此扩展功能在其他布局上也可以正常工作。
但是在此布局和此动画上,它不会为约束变化设置动画,而只是立即应用它。因此,除动画外,布局看起来与我期望的一样—根本没有动画。为什么?
[看起来onViewCreated
中的动画不是最好的主意。最简单的解决方案是在rootLayout.post()
中调用它。