【发布时间】:2020-01-07 10:23:53
【问题描述】:
我的任务是动画按钮从“离开”屏幕,首先到屏幕中心,然后到顶部(参见附件 xml 和屏幕截图)。所以基本上,我必须“链接”2个动画。我的问题是: 我怎样才能只使用 1 个转换?
我使用Transition.TransitionListener 实现了它,使用了doOnEnd() ktx 函数。它工作正常,但代码可能很复杂,因为我还计划删除 onDestroy() 中的侦听器以防止内存泄漏。
这是默认布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<View
android:id="@+id/view"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/cardview_dark_background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintStart_toEndOf="parent"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
这是第一个动画的结果:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<View
android:id="@+id/view"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/cardview_dark_background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
这是第二个动画的结果:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<View
android:id="@+id/view"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/cardview_dark_background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Handler().postDelayed({
val transitionPhase1 = transitionPhase1()
transitionPhase1.doOnEnd {
TransitionManager.beginDelayedTransition(root, transitionPhase2())
}
TransitionManager.beginDelayedTransition(root, transitionPhase1)
}, 2000)
}
private fun transitionPhase1(): Transition {
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.activity_main_phase_1)
constraintSet.applyTo(root)
return AutoTransition()
}
private fun transitionPhase2(): Transition {
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.activity_main_phase_2)
constraintSet.applyTo(root)
return AutoTransition()
}
}
【问题讨论】:
-
是什么触发了这个动画?滚动?
-
在上面提供的代码中,它会在 2 秒延迟后启动。但是我必须将此转换移植到另一个项目,那里的转换是由 http 请求响应触发的。
-
你不能对这种动画使用运动布局,老实说你根本没有使用运动布局,你只是使用了motionlayout标签。 Please read here
标签: android-constraintlayout android-transitions android-motionlayout