Answers:
在执行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-class中,您将创建类似的方法,例如我编写的runFadeAnimation,然后在onCreate中运行它,并且不要忘记将资源ID更改为渐变。
android:duration="@android:integer/config_shortAnimTime"
runFadeAnimation()
从API级别5开始,您可以立即调用overridePendingTransition以指定显式过渡动画:
startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);
要么
finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);
查看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>
@android:anim/fade_in
从Java代码访问?
用 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>
如果您始终希望活动具有相同的过渡动画
@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);
}
}
我想使用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>
// 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);