1.使用overridePendingTransition方法实现Activity跳转动画
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方法中第一个参数是指要跳转的Activity的进入动画,第二个参数指当前Activity的离开动画.
如果不想要某个动画参数直接传0即可
Intent intent = new Intent(this,SixActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.bottom_in,0);
2.使用style的方式定义Activity的切换动画
<!-- 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>// 用于设置打开新的Activity并进入新的Activity展示的动画
<item name="android:activityOpenExitAnimation">@anim/bottom_out</item>// 用于设置打开新的Activity并销毁之前的Activity展示的动画
<item name="android:activityCloseEnterAnimation">@anim/bottom_in</item>// 用于设置关闭当前Activity进入上一个Activity展示的动画
<item name="android:activityCloseExitAnimation">@anim/bottom_out</item>// 用于设置关闭当前Activity时展示的动画
</style>
3.使用ActivityOptions切换动画实现Activity跳转动画(注意:API21以上)
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置contentFeature,可使用切换动画
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单独设置某个Activity的跳转动画
<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>// 用于设置打开新的Activity并进入新的Activity展示的动画
<item name="android:activityOpenExitAnimation">@anim/bottom_out</item>// 用于设置打开新的Activity并销毁之前的Activity展示的动画
<item name="android:activityCloseEnterAnimation">@anim/bottom_in</item>// 用于设置关闭当前Activity进入上一个Activity展示的动画
<item name="android:activityCloseExitAnimation">@anim/bottom_out</item>// 用于设置关闭当前Activity时展示的动画
</style>
5.Fragment之间的跳转动画,Activity之间跳转有默认的跳转动画,而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方法中第一个参数是指添加的fragment的进入动画,第二个参数是指当前framgnet的隐藏动画
transaction.setCustomAnimations(R.anim.bottom_in,R.anim.bottom_out,R.anim.bottom_in,R.anim.bottom_out);
transaction.setCustomAnimations方法重载方法中还有四个参数的方法,其中第三个参数代表返回上一个fragment时进入的动画,第四个参数代表返回上一个fragment时要退出的fragment的退出动画.