我可以更改 Android startActivity() 过渡动画吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3515264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I change the Android startActivity() transition animation?
提问by coneybeare
I am starting an activity and would rather have a alpha fade-in for startActivity()
, and a fade-out for the finish()
. How can I go about this in the Android SDK?
我正在开始一项活动,并且宁愿让 alpha 淡入startActivity()
,而finish()
. 我该如何在 Android SDK 中解决这个问题?
采纳答案by Curtain
In the same statement in which you execute finish(), execute your animation there too. Then, in the new activity, run another animation. See this code:
在执行finish() 的同一语句中,也在那里执行动画。然后,在新活动中,运行另一个动画。看到这个代码:
fadein.xml
淡入淡出.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>
In your finish-class
在你的毕业班
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
淡出.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.
在您的新活动类中,您创建一个类似的方法,例如我编写的 runFadeAnimation,然后在 onCreate 中运行它,不要忘记将资源 ID 更改为淡出。
回答by Allen Chan
Starting from API level 5 you can call overridePendingTransition immediately to specify an explicit transition animation:
从 API 级别 5 开始,您可以立即调用 overridePendingTransition 来指定显式过渡动画:
startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);
or
或者
finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);
回答by monmonja
See themes on android: http://developer.android.com/guide/topics/ui/themes.html.
在 android 上查看主题:http: //developer.android.com/guide/topics/ui/themes.html。
Under themes.xmlthere should be android:windowAnimationStyle
where you can see the declaration of the style in styles.xml.
在themes.xml下android:windowAnimationStyle
,您应该可以看到style.xml中的样式声明。
Example implementation:
示例实现:
<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>
回答by Mohsen mokhtari
Use overridePendingTransition
用 overridePendingTransition
startActivity();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
fadein.xml
淡入淡出.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
<?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>
回答by Cedriga
For fadeInand fadeOut, only add this after super.onCreate(savedInstanceState)in your new Activity class. You don't need to create something else (No XML, no anim folder, no extra function).
对于fadeIn和fadeOut,仅在新Activity 类中的super.onCreate(savedInstanceState)之后添加它。您不需要创建其他东西(没有 XML,没有动画文件夹,没有额外的功能)。
overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
回答by Farid Z
If you always want to the same transition animation for the activity
如果您始终希望活动具有相同的过渡动画
@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);
}
}
回答by Julfikar
You can simply create a context and do something like below:-
您可以简单地创建一个上下文并执行如下操作:-
private Context context = this;
And your animation:-
还有你的动画:-
((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);
You can use any animation you want.
您可以使用任何您想要的动画。
回答by Ashif
// 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);