我正在开展一项活动,宁愿为startActivity()
提供alpha淡入,也为finish()
提供淡出效果。我怎样才能在Android SDK中解决这个问题?
答案 0 :(得分:274)
从API级别5开始,您可以立即调用overridePendingTransition来指定显式过渡动画:
startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);
或
finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);
答案 1 :(得分:36)
查看有关android:http://developer.android.com/guide/topics/ui/themes.html的主题。
在themes.xml下,应该android:windowAnimationStyle
,您可以在styles.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>
答案 2 :(得分:33)
在执行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);
}
fadeout.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>
在你的新Activity类中,你创建一个类似于我编写的runFadeAnimation的类似方法,然后在onCreate中运行它,并且不要忘记将资源id更改为fadeout。
答案 3 :(得分:14)
使用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>
fadeout.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>
答案 4 :(得分:6)
对于 fadeIn 和 fadeOut ,只需在新的Activity类中的 super.onCreate(savedInstanceState)之后添加此内容。您不需要创建其他内容(没有XML,没有动画文件夹,没有额外的功能)。
overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
答案 5 :(得分:5)
您可以简单地创建一个上下文并执行以下操作: -
private Context context = this;
你的动画: -
((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);
您可以使用任何想要的动画。
答案 6 :(得分:4)
如果您始终希望活动具有相同的过渡动画
@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);
}
}
答案 7 :(得分:2)
我想使用styles.xml解决方案,但是它对我的活动不起作用。
事实证明,我不需要使用android:windowEnterAnimation
和android:windowExitAnimation
,而是需要使用以下活动动画:
<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>
此外,由于某些原因,这仅适用于Android 8及更高版本。 我在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(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(activityCloseEnterAnimation, activityCloseExitAnimation))
val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}
答案 8 :(得分:0)
// 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();
tartActivity(myIntent1, bndlanimation1);
答案 9 :(得分:0)
大多数答案都是正确的,但其中一些已被弃用,例如在使用 R.anim.hold 时,其中一些只是详细说明了过程。
因此,您可以使用:
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);