问题描述
我正在整理徽标活动和主活动之间的过渡效果,但是我遇到的问题是,在消失活动之前,该活动移至顶部:
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 $ c内部$ c>)
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);
上面的代码将淡化当前活动的活动
并淡入新启动的活动
,从而实现平稳过渡。
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.
更新:现在,使用转换类执行转换。
UPDATE: It is now common practice to perform transitions using the Transition class introduced in API level 19.
这篇关于如何在“活动”过渡上执行淡入淡出动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!