用动画开始活动 [英] Start Activity with an animation

查看:42
本文介绍了用动画开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-

如果我将 windowAnimationStyle 属性设置为一些自定义样式,该样式定义了 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.

根据参考文档这里

我应该能够在活动开始时使用 activityOpenEnterAnimation 定义动画.但到目前为止还没有成功.

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

有什么想法吗?

推荐答案

我在我当前的一个项目中使用它,它基本上非常简单.您在styles.xml 中定义了一个新的动画样式,如下所示:

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:

<!-- 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>

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

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

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

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

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

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

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

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

这篇关于用动画开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆