我可以改变 Android startActivity()过渡动画吗?

我开始一个活动,宁愿有一个阿尔法淡入为 startActivity(),淡出为 finish()。我如何在 Android SDK 中实现这一点?

156126 次浏览

请参阅 android 上的主题: http://developer.android.com/guide/topics/ui/themes.html

Themes.xml下面应该有 android:windowAnimationStyle,在这里您可以看到 Xml中的样式声明。

实施例子:

<style name="AppTheme" parent="...">


...


<item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>


</style>


<style name="WindowAnimationStyle">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>

在执行 Finish ()的相同语句中,也在那里执行动画。然后,在新活动中运行另一个动画。看这个代码:

Fadein.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"/> //Time in milliseconds
</set>

在你的终极课堂上

private void finishTask() {
if("blabbla".equals("blablabla"){
finish();
runFadeInAnimation();
}
}


private void runFadeInAnimation() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.fadein);
a.reset();
LinearLayout ll = (LinearLayout) findViewById(R.id.yourviewhere);
ll.clearAnimation();
ll.startAnimation(a);
}

Xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"/>
</set>

In your new Activity-class you create a similiar method like the runFadeAnimation I wrote and then you run it in onCreate and don't forget to change the resources id to fadeout.

从 API 级别5开始,您可以立即调用 overridePendingTransfer 来指定一个显式的转换动画:

startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);

或者

finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);

您可以简单地创建一个上下文,然后执行以下操作:-

private Context context = this;

和你的动画:-

((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);

你可以使用任何你想要的动画。

使用 overridePendingTransition

startActivity();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);

Fadein.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<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" />
</set>

Xml

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

对于 褪色淡出,只能在 OnCreate (savedInstanceState)之后添加到新的 Activity 类中。您不需要创建其他内容(不需要 XML、不需要 anim 文件夹、不需要额外的函数)。

overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);

如果您总是希望活动有相同的过渡动画

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);


@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
 // CREATE anim


// CREATE animation,animation2  xml // animation like fade out


Intent myIntent1 = new Intent(getApplicationContext(), Attend.class);
Bundle bndlanimation1 =  ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation, R.anim.animation2).toBundle();
startActivity(myIntent1, bndlanimation1);

我想使用 styles.xml 解决方案,但是它不适合我的活动。 Turns out that instead of using android:windowEnterAnimation and android:windowExitAnimation, I need to use the activity animations like this:

<style name="ActivityAnimation.Vertical" parent="">
<item name="android:activityOpenEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityOpenExitAnimation">@anim/exit_to_bottom</item>
<item name="android:activityCloseEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityCloseExitAnimation">@anim/exit_to_bottom</item>
<item name="android:windowEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:windowExitAnimation">@anim/exit_to_bottom</item>
</style>

然后我在我的主题中提到:

<style name="AppTheme">
...
<item name="android:windowAnimationStyle">@style/ActivityAnimation.Vertical</item>
...
</style>

Also, for some reason this only worked from Android 8 and above. 我将下面的代码添加到我的 BaseActivity 中,以修复下面的 API 级别:

override fun finish() {
super.finish()
setAnimationsFix()
}


/**
* The activityCloseExitAnimation and activityCloseEnterAnimation properties do not work correctly when applied from the theme.
* So in this fix, we retrieve them from the theme, and apply them.
* @suppress Incorrect warning: https://stackoverflow.com/a/36263900/1395437
*/
@SuppressLint("ResourceType")
private fun setAnimationsFix() {
// Retrieve the animations set in the theme applied to this activity in the manifest..
var activityStyle = theme.obtainStyledAttributes(intArrayOf(android.R.attr.windowAnimationStyle))
val windowAnimationStyleResId = activityStyle.getResourceId(0, 0)
activityStyle.recycle()
// Now retrieve the resource ids of the actual animations used in the animation style pointed to by
// the window animation resource id.
activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation))
val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}

Most of the answers are pretty correct, but some of them are deprecated such as when using R.anim.hold and some of them are just elaboratig the process.

因此,你可以使用:

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