为内置和自定义预测性返回动画添加支持

如果您已将应用迁移到新的系统返回 API,您可以选择启用预测性返回,以自动接收应用内动画并支持自定义过渡。

添加对内置应用内动画的支持

视频:预测性返回动画

启用后,您的应用会显示返回主屏幕、跨 Activity 和跨任务的动画。

您还可以将您的 Material Component 依赖项升级到 MDC Android 的 v1.10.0 版本,以接收以下 Material Component 动画:

如需了解更多信息,请参阅 GitHub 上的 Material Component 开发者指南

该视频简要展示了使用 Android 设置应用进行跨 Activity 和返回主屏幕的预测性返回动画示例。

  1. 在动画中,用户向后滑动以返回上一个设置屏幕——这是一个跨 Activity 动画的示例。
  2. 现在在之前的屏幕上,用户第二次开始向后滑动,显示带有壁纸的主屏幕预览——这是一个返回主屏幕动画的示例。
  3. 用户继续向右滑动,显示窗口缩小到主屏幕图标的动画。
  4. 用户现在已完全返回主屏幕。

详细了解如何支持预测性返回

添加自定义应用内过渡和动画

您可以使用 Progress API 和自定义跨 Activity 动画方法 overrideActivityTransition 创建自定义应用内属性动画和过渡。

使用 Progress API 添加自定义过渡

借助 AndroidX Activity 1.8.0-alpha01 或更高版本,您可以使用预测性返回进度 API 为应用中的预测性返回手势开发自定义动画。在 OnBackPressedCallback 中,我们引入了 handleOnBackProgressedhandleOnBackCancelledhandleOnBackStarted 方法,以便在用户向后滑动时为对象设置动画。如果新系统动画或 Material Component 动画提供的默认动画不能满足您的自定义需求,请使用这些方法。

我们预计大多数应用会使用向后兼容的 AndroidX API,但在 Android 14 开发者预览版 1 及更高版本中,OnBackAnimationCallback 接口中也有类似的平台 API 可供测试。

将 Progress API 与 AndroidX Transitions 结合使用

Progress API 可与 AndroidX Transitions 1.5.0-alpha01 或更高版本(在 Android 14 及更高版本上)结合使用,以创建预测性返回过渡。

  1. 使用 TransitionManager#controlDelayedTransition 而非 beginDelayedTransition 来在用户向后滑动时播放过渡。
  2. handleOnBackStarted 中创建过渡。
  3. handleOnBackProgressed 中,通过关联 currentFractionBackEvent.progress(该属性会公开用户向后滑动的距离),播放与返回事件相关的过渡。
  4. handleOnBackPressed 中,在用户完成返回手势后,结束过渡。
  5. 最后,在 handleOnBackCancelled 中重置过渡状态。

以下视频、Kotlin 代码和 XML 演示了使用 OnBackPressedCallback 实现的两个框之间的自定义过渡。

class MyFragment : Fragment() {

    val transitionSet = TransitionSet().apply {
        addTransition(Fade(Fade.MODE_OUT))
        addTransition(ChangeBounds())
        addTransition(Fade(Fade.MODE_IN))
    }
    ...
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val callback = object : OnBackPressedCallback(enabled = false) {

            var controller: TransitionSeekController? = null

            @RequiresApi(34)
            override fun handleOnBackStarted(backEvent: BackEvent) {
                // Create the transition
                controller = TransitionManager.controlDelayedTransition(
                    binding.card,
                    transitionSet
                )
                changeTextVisibility(ShowText.SHORT)
            }

            @RequiresApi(34)
            override fun handleOnBackProgressed(backEvent: BackEvent) {
                // Play the transition as the user swipes back
                if (controller?.isReady == true) {
                    controller?.currentFraction = backEvent.progress
                }
            }

            override fun handleOnBackPressed() {
                // Finish playing the transition when the user commits back
                controller?.animateToEnd()
                this.isEnabled = false
            }

            @RequiresApi(34)
            override fun handleOnBackCancelled() {
                // If the user cancels the back gesture, reset the state
                transition(ShowText.LONG)
            }
        }

        binding.shortText.setOnClickListener {
            transition(ShowText.LONG)
            callback.isEnabled = true
        }

        this.requireActivity().onBackPressedDispatcher.addCallback(callback)
    }

    private fun transition(showText: ShowText) {
        TransitionManager.beginDelayedTransition(
            binding.card,
            transitionSet
        )
        changeTextVisibility(showText)
    }

    enum class ShowText { SHORT, LONG }
    private fun changeTextVisibility(showText: ShowText) {
        when (showText) {
            ShowText.SHORT -> {
                binding.shortText.isVisible = true
                binding.longText.isVisible = false
            }
            ShowText.LONG -> {
                binding.shortText.isVisible = false
                binding.longText.isVisible = true
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
...
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...>

        <TextView
            android:id="@+id/short_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            ... />

        <TextView
            android:id="@+id/long_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            .../>

    </androidx.constraintlayout.widget.ConstraintLayout>

使用预测性返回过渡时请注意以下事项:

  • 使用 isSeekingSupported 检查过渡是否支持预测性返回。
  • 为您的自定义过渡重写 isSeekingSupported 以返回 true。
  • 每个动画创建一个控制器。
  • 预测性返回过渡支持 AndroidX 过渡,但不支持框架过渡。我们建议您从框架过渡迁移。
  • 预测性返回过渡在 Android 14 及更高版本设备上受支持,并且不向后兼容。
  • 使用 XML 场景创建的过渡也受支持。在 handleOnBackStarted 中,将您的 TransitionSeekController 设置为 TransitionManager.createSeekController 的结果,而不是 controlDelayedTransition 的结果。

在 Android 14 及更高版本上添加自定义 Activity 过渡

为确保自定义 Activity 过渡在 Android 14 及更高版本上支持预测性返回,您可以使用 overrideActivityTransition 而非 overridePendingTransition。这意味着过渡动画会在用户向后滑动时播放。

为了提供一个示例说明其工作原理,假设 Activity B 在返回堆栈中位于 Activity A 的顶部。您将按以下方式处理自定义 Activity 动画:

  • 在 Activity B 的 onCreate 方法中调用打开或关闭过渡。
  • 当用户导航到 Activity B 时,使用 OVERRIDE_TRANSITION_OPEN。当用户滑动返回 Activity A 时,使用 OVERRIDE_TRANSITION_CLOSE
  • 指定 OVERRIDE_TRANSITION_CLOSE 时,enterAnim 是 Activity A 的进入动画,exitAnim 是 Activity B 的退出动画。