gpt4 book ai didi

android - 当 Activity 首次加载时 TransitionManager.beginDelayedTransition 不会产生动画

转载 作者:行者123 更新时间:2023-12-02 17:05:22 31 4
gpt4 key购买 nike

我使用 ConstraintLayout 和 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 而没有任何动画

enter image description here

我上面的做法有什么问题吗?

最佳答案

来自documentation 对于beginDelayedTransition:

Calling this method causes TransitionManager to capture current values in the scene root and then post a request to run a transition on the next frame. At that time, the new values in the scene root will be captured and changes will be animated.

您必须等到布局布置完毕后才能尝试转换。有多种方法可以做到这一点,但最简单的方法是发布转换代码,如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val constraintLayout = findViewById<ConstraintLayout>(R.id.constraintLayout)
constraintLayout.post {
val constraintSet1 = ConstraintSet()
constraintSet1.clone(constraintLayout)
val constraintSet2 = ConstraintSet()
constraintSet2.clone(this, R.layout.activity_main2_alt)
val transition = ChangeBounds()
transition.duration = 5000
TransitionManager.beginDelayedTransition(constraintLayout, transition)
constraintSet2.applyTo(constraintLayout)
}
}

关于android - 当 Activity 首次加载时 TransitionManager.beginDelayedTransition 不会产生动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55704802/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com