This repo will introduce to how make fragment transaction animation like that:
When replace FragmentTow, we will custom setCustomAnimations method as below:
private void showFragmentTwo() {
Fragment fragmentTwo = new FragmentTwo();
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit)
.replace(R.id.container, fragmentTwo)
.addToBackStack("FragmentTwo")
.commit();
}
At res/anim, we will create 4 animation files:
- enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0" />
</set>
- exit.xml
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="-100%" />
</set>
- pop_enter.xml
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="-100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0" />
</set>
- pop_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="100%" />
</set>