1.使用overridePendingTransition方法實(shí)現(xiàn)Activity跳轉(zhuǎn)動(dòng)畫
botton_in
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:duration="400">
</translate>
bottom_out
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="400">
</translate>
public void go(View view){
Intent intent = new Intent(this,SixActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.bottom_in,R.anim.bottom_out);
}
overridePendingTransition方法中第一個(gè)參數(shù)是指要跳轉(zhuǎn)的Activity的進(jìn)入動(dòng)畫,第二個(gè)參數(shù)指當(dāng)前Activity的離開動(dòng)畫.
如果不想要某個(gè)動(dòng)畫參數(shù)直接傳0即可
Intent intent = new Intent(this,SixActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.bottom_in,0);
2.使用style的方式定義Activity的切換動(dòng)畫
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/BOTTOM_TO_TOP</item>
</style>
<style name="BOTTOM_TO_TOP">
<item name="android:activityOpenEnterAnimation">@anim/bottom_in</item>// 用于設(shè)置打開新的Activity并進(jìn)入新的Activity展示的動(dòng)畫
<item name="android:activityOpenExitAnimation">@anim/bottom_out</item>// 用于設(shè)置打開新的Activity并銷毀之前的Activity展示的動(dòng)畫
<item name="android:activityCloseEnterAnimation">@anim/bottom_in</item>// 用于設(shè)置關(guān)閉當(dāng)前Activity進(jìn)入上一個(gè)Activity展示的動(dòng)畫
<item name="android:activityCloseExitAnimation">@anim/bottom_out</item>// 用于設(shè)置關(guān)閉當(dāng)前Activity時(shí)展示的動(dòng)畫
</style>
3.使用ActivityOptions切換動(dòng)畫實(shí)現(xiàn)Activity跳轉(zhuǎn)動(dòng)畫(注意:API21以上)
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設(shè)置contentFeature,可使用切換動(dòng)畫
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Transition explode = TransitionInflater.from(this).inflateTransition(android.R.transition.explode);
getWindow().setEnterTransition(explode);
setContentView(R.layout.activity_five);
}
public void go(View view){
Intent intent = new Intent(this,SixActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
}
4.通過AndroidManifest單獨(dú)設(shè)置某個(gè)Activity的跳轉(zhuǎn)動(dòng)畫
<activity android:name=".SixActivity" android:theme="@style/myTheme"/>
<style name="myTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/BOTTOM_TO_TOP</item>
</style>
<style name="BOTTOM_TO_TOP">
<item name="android:activityOpenEnterAnimation">@anim/bottom_in</item>// 用于設(shè)置打開新的Activity并進(jìn)入新的Activity展示的動(dòng)畫
<item name="android:activityOpenExitAnimation">@anim/bottom_out</item>// 用于設(shè)置打開新的Activity并銷毀之前的Activity展示的動(dòng)畫
<item name="android:activityCloseEnterAnimation">@anim/bottom_in</item>// 用于設(shè)置關(guān)閉當(dāng)前Activity進(jìn)入上一個(gè)Activity展示的動(dòng)畫
<item name="android:activityCloseExitAnimation">@anim/bottom_out</item>// 用于設(shè)置關(guān)閉當(dāng)前Activity時(shí)展示的動(dòng)畫
</style>
5.Fragment之間的跳轉(zhuǎn)動(dòng)畫,Activity之間跳轉(zhuǎn)有默認(rèn)的跳轉(zhuǎn)動(dòng)畫咙崎,而Fragment需要自己定義
public void addFragment(Fragment fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.bottom_in,R.anim.bottom_out);
transaction.replace(R.id.fragment_container,fragment).addToBackStack("").commitAllowingStateLoss();
}
public void removeFragment(){
getSupportFragmentManager().popBackStack();
}
transaction.setCustomAnimations方法中第一個(gè)參數(shù)是指添加的fragment的進(jìn)入動(dòng)畫褪猛,第二個(gè)參數(shù)是指當(dāng)前framgnet的隱藏動(dòng)畫
transaction.setCustomAnimations(R.anim.bottom_in,R.anim.bottom_out,R.anim.bottom_in,R.anim.bottom_out);
transaction.setCustomAnimations方法重載方法中還有四個(gè)參數(shù)的方法,其中第三個(gè)參數(shù)代表返回上一個(gè)fragment時(shí)進(jìn)入的動(dòng)畫伊滋,第四個(gè)參數(shù)代表返回上一個(gè)fragment時(shí)要退出的fragment的退出動(dòng)畫.