【发布时间】:2019-04-16 09:26:16
【问题描述】:
我正在通过 TransitionManager 使用带有动画的约束布局。
我有以下两种布局
这是我的main_activity.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
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/image"
android:layout_width="0dp"
android:layout_height="160dp"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:text="Let's start animating"/>
</android.support.constraint.ConstraintLayout>
这是我的main_activity_alt.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="160dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:text="Let's start animating"/>
</android.support.constraint.ConstraintLayout>
在我的 MainActivity 中,我想让图像向上滑动,这将是 main_activity_alt.xml
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val constraintSet1 = ConstraintSet()
constraintSet1.clone(constraintLayout)
val constraintSet2 = ConstraintSet()
constraintSet2.clone(this, R.layout.activity_main_alt)
val transition = ChangeBounds()
transition.duration = 5000
val handler = Handler()
handler.postDelayed({
TransitionManager.beginDelayedTransition(constraintLayout, transition)
constraintSet2.applyTo(constraintLayout)
}, 10)
}
当上述开始时,图像已经显示。 main_activity.xml 应该隐藏起来,main_activity_alt.xml 将是它的最后安息之地。
但是,当屏幕加载时,它会立即显示 imageview 而没有任何动画
我在上面做错了什么吗?
【问题讨论】:
-
我认为必须渲染第一个布局,以便您可以动画过渡到第二个布局。尝试在视图创建之后而不是之前做动画
-
我已将代码添加到 onResume。但是,图像始终会立即显示。
标签: android android-animation android-constraintlayout android-transitions