Android 用动画开始活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2625812/
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 06:38:54  来源:igfitidea点击:

Start Activity with an animation

androidanimationandroid-activitytransition

提问by adityad

I am trying to start an activity with a custom transition animation. The only way I have found out so far to do this (without using onPendingTransition() in the previous activity) is to use a custom theme on the activity and define either activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle to set the animation. But, none of these attributes are working for me. Some experimentation yielded the following results-

我正在尝试使用自定义过渡动画开始活动。到目前为止,我发现的唯一方法(在上一个活动中不使用 onPendingTransition() )是在活动上使用自定义主题并定义 activityOpenEnterAnimation、taskOpenEnterAnimation、windowEnterAnimation 或 windowAnimationStyle 来设置动画。但是,这些属性都不适合我。一些实验产生了以下结果-

If I set the windowAnimationStyle attribute to some custom style which defines values for activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle I can get rid of the default transition animation occurring at the start of the activity. It doesn't show the transition animation using the actual value specified but at least the default animation is not shown.

如果我将 windowAnimationStyle 属性设置为一些自定义样式,该样式定义了 activityOpenEnterAnimation、taskOpenEnterAnimation、windowEnterAnimation 或 windowAnimationStyle 的值,我可以摆脱在活动开始时发生的默认过渡动画。它不会使用指定的实际值显示过渡动画,但至少不会显示默认动画。

According to the reference doc here,

根据这里的参考文档,

I should be able to define an animation at the start of the activity using activityOpenEnterAnimation. But no success so far.

我应该能够在活动开始时使用 activityOpenEnterAnimation 定义动画。但至今没有成功。

Any ideas?

有任何想法吗?

回答by mreichelt

I am using this in a current project of mine, it is basically pretty simple. You define a new animation style in your styles.xml, like this:

我在我当前的一个项目中使用它,它基本上很简单。你在你的 style.xml 中定义了一个新的动画样式,像这样:

<!-- just defines top layer "Animation" -->
<style name="Animation" />

<!-- the animations must have been defined in your "anim" folder, of course -->
<style name="Animation.MyAwesomeAnimation" parent="android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/myawesomeanimation_enter</item>
    <item name="android:activityOpenExitAnimation">@anim/hold_long</item>
    <item name="android:activityCloseEnterAnimation">@anim/hold_long</item>
    <item name="android:activityCloseExitAnimation">@anim/myawesomeanimation_exit</item>
</style>

Then set this style in a theme (themes.xml):

然后在主题 (themes.xml) 中设置此样式:

<style name="Theme.MyAwesomeTheme" parent="Theme.Default">
    <item name="android:windowAnimationStyle">@style/Animation.MyAwesomeAnimation</item>
</style>

And then you can simply set these themes to every activity you like in your AndroidManifest.xml:

然后您可以简单地将这些主题设置为您在 AndroidManifest.xml 中喜欢的每个活动:

<activity
    android:name=".MyAwesomeActivity"
    android:theme="@style/Theme.MyAwesomeTheme" />

Now I wish you big fun with activity animations! :-D

现在我祝你在活动动画中玩得开心!:-D