使用动画启动 activity

Material Design 应用中的 Activity 过渡通过常见元素之间的运动和转换,在不同状态之间提供视觉联系。您可以为进入和退出过渡,以及 activity 之间共享元素的过渡指定自定义动画。

图 1. 包含共享元素的过渡。

  • 进入过渡决定了 activity 中的视图如何进入场景。例如,在 explode 进入过渡中,视图从外面进入场景并向内飞向屏幕的中心。
  • 退出过渡决定了 activity 中的视图如何退出场景。例如,在 explode 退出过渡中,视图会从中心退出场景。
  • 共享元素过渡决定了两个 activity 之间共享的视图如何在这些 activity 之间过渡。例如,如果两个 activity 具有相同的图片(但位置和大小不同),则 changeImageTransform 共享元素过渡会在这些 activity 之间流畅地平移和缩放图片。

Android 支持以下进入和退出过渡:

  • explode:将视图移入或移出场景中心。
  • slide:将视图移入或移出场景的其中一个边缘。
  • fade:通过更改视图的不透明度在场景中添加或移除视图。

系统支持将任何扩展 Visibility 类的过渡作为进入或退出过渡。如需了解详情,请参阅 Transition 类的 API 参考文档。

Android 还支持以下共享元素过渡:

  • changeBounds:为目标视图的布局边界的变化添加动画效果。
  • changeClipBounds:为目标视图裁剪边界的变化添加动画效果。
  • changeTransform:为目标视图缩放和旋转方面的变化添加动画效果。
  • changeImageTransform:为目标图片的大小和缩放变化添加动画效果。

当您在应用中启用 activity 过渡时,默认的交替淡变过渡会在进入和退出 activity 之间激活。

图 2. 具有一个共享元素的场景过渡。

如需查看为使用共享元素的 activity 添加动画效果的示例代码,请参阅 ActivitySceneTransitionBasic

检查系统版本

Activity 过渡 API 适用于 Android 5.0 (API 21) 及更高版本。如需保持与早期版本的 Android 兼容,请先在运行时检查系统 version,然后再针对以下任何功能调用 API:

Kotlin

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Apply activity transition
} else {
    // Swap without transition
}

Java

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Apply activity transition
} else {
    // Swap without transition
}

指定自定义过渡

首先,在定义从 Material 主题继承的样式时,请使用 android:windowActivityTransitions 属性启用窗口内容转换。您还可以在样式定义中指定进入、退出和共享元素过渡:

<style name="BaseAppTheme" parent="android:Theme.Material">
  <!-- enable window content transitions -->
  <item name="android:windowActivityTransitions">true</item>

  <!-- specify enter and exit transitions -->
  <item name="android:windowEnterTransition">@transition/explode</item>
  <item name="android:windowExitTransition">@transition/explode</item>

  <!-- specify shared element transitions -->
  <item name="android:windowSharedElementEnterTransition">
    @transition/change_image_transform</item>
  <item name="android:windowSharedElementExitTransition">
    @transition/change_image_transform</item>
</style>

此示例中的 change_image_transform 过渡定义如下:

<!-- res/transition/change_image_transform.xml -->
<!-- (see also Shared Transitions below) -->
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
  <changeImageTransform/>
</transitionSet>

changeImageTransform 元素对应于 ChangeImageTransform 类。如需了解详情,请参阅 Transition 的 API 参考文档。

如需改为在代码中启用窗口内容转换,请调用 Window.requestFeature() 函数:

Kotlin

// Inside your activity (if you did not enable transitions in your theme)
with(window) {
    requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

    // Set an exit transition
    exitTransition = Explode()
}

Java

// Inside your activity (if you did not enable transitions in your theme)
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);

// Set an exit transition
getWindow().setExitTransition(new Explode());

如需在代码中指定过渡,请使用 Transition 对象调用以下函数:

setExitTransition()setSharedElementExitTransition() 函数用于定义正在调用的 activity 的退出过渡。setEnterTransition()setSharedElementEnterTransition() 函数用于定义之前调用的 activity 的进入过渡。

为了获得完整的过渡效果,您必须在正在调用的 activity 和被调用的 activity 上启用窗口内容过渡。否则,正在调用的 activity 会开始退出过渡,但随后您会看到窗口过渡,如缩放或淡出。

如需尽快开始进入过渡,请对被调用的 activity 使用 Window.setAllowEnterTransitionOverlap() 函数。这样一来,您的进入过渡效果会更加精彩。

启动使用过渡的 Activity

如果您启用转换并为某个 activity 设置退出转换,转换会在您启动另一个 activity 时激活,如下所示:

Kotlin

startActivity(intent,
              ActivityOptions.makeSceneTransitionAnimation(this).toBundle())

Java

startActivity(intent,
              ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

如果您为第二个 activity 设置进入过渡,该过渡也会在 activity 启动时激活。如需在启动另一个 activity 时停用过渡,请提供 null 选项包。

启动具有一个共享元素的 Activity

如需在具有共享元素的两个 activity 之间添加屏幕过渡动画,请执行以下操作:

  1. 在主题背景中启用窗口内容过渡。
  2. 在样式中指定共享元素过渡。
  3. 将过渡定义为 XML 资源。
  4. 使用 android:transitionName 属性为两个布局中的共享元素指定一个通用名称。
  5. 使用 ActivityOptions.makeSceneTransitionAnimation() 函数。

Kotlin

// Get the element that receives the click event
val imgContainerView = findViewById<View>(R.id.img_container)

// Get the common element for the transition in this activity
val androidRobotView = findViewById<View>(R.id.image_small)

// Define a click listener
imgContainerView.setOnClickListener( {
    val intent = Intent(this, Activity2::class.java)
    // Create the transition animation - the images in the layouts
    // of both activities are defined with android:transitionName="robot"
    val options = ActivityOptions
            .makeSceneTransitionAnimation(this, androidRobotView, "robot")
    // Start the new activity
    startActivity(intent, options.toBundle())
})

Java

// Get the element that receives the click event
final View imgContainerView = findViewById(R.id.img_container);

// Get the common element for the transition in this activity
final View androidRobotView = findViewById(R.id.image_small);

// Define a click listener
imgContainerView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, Activity2.class);
        // Create the transition animation - the images in the layouts
        // of both activities are defined with android:transitionName="robot"
        ActivityOptions options = ActivityOptions
            .makeSceneTransitionAnimation(this, androidRobotView, "robot");
        // Start the new activity
        startActivity(intent, options.toBundle());
    }
});

对于在代码中生成的共享动态视图,请使用 View.setTransitionName() 函数在两个 activity 中指定通用元素名称。

如需在完成第二个 activity 时反转场景过渡动画,请调用 Activity.finishAfterTransition() 函数,而非 Activity.finish()

启动具有多个共享元素的 Activity

如需在两个具有多个共享元素的 activity 之间制作场景过渡动画,请使用 android:transitionName 属性在两个布局中定义共享元素(或在两个 activity 中使用 View.setTransitionName() 函数),然后创建一个 ActivityOptions 对象,如下所示:

Kotlin

// Rename the Pair class from the Android framework to avoid a name clash
import android.util.Pair as UtilPair
...
val options = ActivityOptions.makeSceneTransitionAnimation(this,
        UtilPair.create(view1, "agreedName1"),
        UtilPair.create(view2, "agreedName2"))

Java

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,
        Pair.create(view1, "agreedName1"),
        Pair.create(view2, "agreedName2"));