Android 使用 ChangeImageTransform 共享元素转换在两个 Activity 之间设置 ImageView 动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26600239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:56:18  来源:igfitidea点击:

Animate ImageView between two activities using shared element transitions with ChangeImageTransform

androidandroid-5.0-lollipopshared-element-transitionactivity-transition

提问by iForests

I am trying to animate one ImageView to another position between two activities in Android API level 21. Since "MoveImage" in Android L Preview has been removed, I use "ChangeImageTransform" instead, but the sample code in documents doesn't work out (the two images animated separately).

我试图在 Android API 级别 21 中将一个 ImageView 动画到两个活动之间的另一个位置。由于 Android L Preview 中的“MoveImage”已被删除,我改用“ ChangeImageTransform”,但文档中的示例代码不起作用(两个图像分别动画)。

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeImageTransform>
        <targets>
            <target android:targetId="@id/ivA" />
            <target android:targetId="@id/ivB" />
        </targets>
    </changeImageTransform>
</transitionSet>

Is there any working example? Thanks!

有没有工作的例子?谢谢!

回答by Yi-Ping Shih

To make a screen transition animation between two activities that have a shared element, you can read this articleand follow the mentioned steps:

要在具有共享元素的两个活动之间制作屏幕过渡动画,您可以阅读本文并按照上述步骤操作:

  1. Enable window content transitions in your theme.
  2. Specify a shared elements transition in your style.
  3. Define your transition as an XML resource.
  4. Assign a common name to the shared elements in both layouts with the android:transitionName attribute.
  5. Use the ActivityOptions.makeSceneTransitionAnimation() method.
  1. 在您的主题中启用窗口内容转换。
  2. 在您的样式中指定共享元素过渡。
  3. 将您的转换定义为 XML 资源。
  4. 使用 android:transitionName 属性为两个布局中的共享元素分配一个通用名称。
  5. 使用 ActivityOptions.makeSceneTransitionAnimation() 方法。

About the 3rd step, according to the documentation:

关于第三步,根据文档

In combination with ChangeBounds, ChangeImageTransform allows ImageViews that change size, shape, or ImageView.ScaleType to animate contents smoothly.

与 ChangeBounds 结合,ChangeImageTransform 允许更改大小、形状或 ImageView.ScaleType 的 ImageViews 平滑地为内容设置动画。

The res/transition/your_transition.xml should be like this:

res/transition/your_transition.xml 应该是这样的:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds>
        <targets>
            <target android:targetId="@id/ivA" />
            <target android:targetId="@id/ivB" />
        </targets>
    </changeBounds>
    <changeImageTransform>
        <targets>
            <target android:targetId="@id/ivA" />
            <target android:targetId="@id/ivB" />
        </targets>
    </changeImageTransform>
</transitionSet>

or simply like this if only ivA and ivB need to be animated:

或者只是这样,如果只需要 ivA 和 ivB 需要动画:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds/>
    <changeImageTransform/>
</transitionSet>

回答by formica

I have got this working following this guide, a few others, and the referenced material.

我按照本指南、其他一些指南和参考资料进行了这项工作。

A transition set in this style. I put this under res/transition:

以这种风格设置的过渡。我把它放在 res/transition 下:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds>
        <targets>
            <target android:targetId="@id/ivA" />
            <target android:targetId="@id/ivB" />
        </targets>
    </changeBounds>
    <changeImageTransform>
        <targets>
            <target android:targetId="@id/ivA" />
            <target android:targetId="@id/ivB" />
        </targets>
    </changeImageTransform>
</transitionSet>

In the source and target ImageViews, you need to add a name tag. The name must be the same.

在源和目标 ImageViews 中,需要添加名称标签。名称必须相同。

<ImageView
...
android:transitionName="MYTRANSITIONVIEW"

/>

In styles.xml, add to the application theme:

在styles.xml中,添加到应用主题:

<item name="android:windowContentTransitions">true</item>
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowSharedElementEnterTransition">@transition/my_transition</item>
<item name="android:windowSharedElementExitTransition">@transition/my_transition</item>

I am running this inside a fragment so I start the new activity like this:

我在一个片段中运行它,所以我开始这样的新活动:

Bundle bundle = null;

if (activity != null) {
    ActivityOptionsCompat options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, Frag2_, "MYTRANSITIONVIEW");
    bundle = options.toBundle();
}
activity.startActivity(i, bundle);

It works on API 21 clients. It did not work on an API 16 client as the XML tags are not valid.

它适用于 API 21 客户端。它不适用于 API 16 客户端,因为 XML 标记无效。

I hope this helps.

我希望这有帮助。

Slight update, to get the reverse transition on exit, I had to call

轻微更新,为了在退出时获得反向转换,我不得不打电话

supportFinishAfterTransition();

rather than finish();

而不是完成();