如何在活动过渡上执行淡入淡出动画? [英] How to perform a fade animation on Activity transition?

查看:35
本文介绍了如何在活动过渡上执行淡入淡出动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编码我的徽标活动和我的主要活动之间的过渡效果,但我有一个问题,即在活动消失之前移动到顶部:

I am codifiying a transition effect between my logo activity and my Main activity, but I have the problem that before vanish the activity move to top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>

</set>

我怎样才能改进这段代码以获得消失的效果?

How could I improve this code to get only a vanish effect?

推荐答案

您可以创建自己的 .xml 动画文件来淡入新的 Activity 并淡出当前Activity:

You could create your own .xml animation files to fade in a new Activity and fade out the current Activity:

fade_in.xml

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="0.0" android:toAlpha="1.0"
           android:duration="500" />

fade_out.xml

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="1.0" android:toAlpha="0.0"
           android:fillAfter="true"
           android:duration="500" />

在这样的代码中使用它:(在你的 Activity 中)

Use it in code like that: (Inside your Activity)

Intent i = new Intent(this, NewlyStartedActivity.class);
startActivity(i);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

以上代码将淡出当前活动的 Activity 并淡入新启动的 Activity,从而实现平滑过渡.

The above code will fade out the currently active Activity and fade in the newly started Activity resulting in a smooth transition.

更新:@Dan J 指出使用内置的 Android 动画提高性能,我在做了一些测试后发现确实如此.如果您更喜欢使用内置动画,请使用:

UPDATE: @Dan J pointed out that using the built in Android animations improves performance, which I indeed found to be the case after doing some testing. If you prefer working with the built in animations, use:

overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

注意我引用 android.R 而不是 R 来访问资源 ID.

Notice me referencing android.R instead of R to access the resource id.

更新:现在使用 API 级别 19 中引入的转换类.

这篇关于如何在活动过渡上执行淡入淡出动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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