Activity的打開關閉或者說相互跳轉之間可以設置動畫的。默認的打開關閉直接消失或出現,比較不優美,但是有的手機Rom對這個默認做了修改,比如紅米HM1,默認的就是新頁面自右向左滑動出現,自左向右滑動消失。
設置動畫有兩種方法:
1。利用Activity的方法在代碼中設置:
public void overridePendingTransition (int enterAnim, int exitAnim)
Call immediately after one of the flavors ofstartActivity(Intent)
orfinish()
to specify an explicit transition animation to perform next.
enterAnimA resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnimA resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
enterAnimA在此Actvity是將要出現的Activity時的進入動畫。
exitAnimA在此Actvity是當前將要退出的Activity時的退出動畫。
這個方法一定要在activity的start和finish之后立即調用。

@Override public void finish() { super.finish(); if (isCloseAnim) { this.overridePendingTransition(0, R.anim.activity_out_to_up); } }

Intent intent = new Intent(context, QuizActivity.class); intent.putExtra(EXTRA_SCHEME_ID, schemeId); context.startActivity(intent); //設置切換動畫,自下而上進入,自上而下退出 ((Activity) context).overridePendingTransition(R.anim.activity_in_from_down, 0);
2。設置Activity的主題風格,在xml中實現:
在AndroidManifest里面,對於application和activity標簽可以定義theme屬性。如果對Application定義了某一個屬性,那么會對所有的activity產生影響,當然可以在activity中覆蓋它,為這個Activity添加自己的特定屬性。
<application android:theme="@style/ThemeActivity">
然后在values/themes.xml中定義ThemeActivity這個主題
<style name="ThemeActivity" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowAnimationStyle">@style/AnimationActivity</item>
<item name="android:windowNoTitle">true</item>
</style>
在values/styles.xml中定義AnimationActivity的style
<style name="AnimationActivity" parent="@android:style/Animation.Activity" > <item name="android:activityOpenEnterAnimation">@anim/push_left_in</item> <item name="android:activityOpenExitAnimation">@anim/push_left_out</item> <item name="android:activityCloseEnterAnimation">@anim/push_right_in</item> <item name="android:activityCloseExitAnimation">@anim/push_right_out</item> </style>
至於anim中的動畫在res/anim下用xml的set屬性定義好就可以了。各個動畫item的意義如下:
android:activityCloseEnterAnimation When closing the current activity, this is the animation that is run on the next activity (which is entering the screen). android:activityCloseExitAnimation When closing the current activity, this is the animation that is run on the current activity (which is exiting the screen). android:activityOpenEnterAnimation When opening a new activity, this is the animation that is run on the next activity (which is entering the screen). android:activityOpenExitAnimation When opening a new activity, this is the animation that is run on the previous activity (which is exiting the screen).
Dialog和PopupWindow也可以設置動畫。
比如在定義PopupWindow時指定動畫:
setAnimationStyle(R.style.mypopwindow_anim_bottom_style);