Fragment的切换动画实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void addFragment() {
if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
// 标准动画
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
// 自定义动画
// API LEVEL 11
// fragmentTransaction.setCustomAnimations(
// R.animator.fragment_slide_left_enter,
// R.animator.fragment_slide_right_exit);
// API LEVEL 13
fragmentTransaction.setCustomAnimations(
R.animator.fragment_left_enter,
R.animator.fragment_left_exit,
R.animator.fragment_pop_left_enter,
R.animator.fragment_pop_left_exit);
fragmentTransaction.add(R.id.container, mTextFragmentOne);
// 加入到BackStack中
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void removeFragment() {
if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mFragmentManager.popBackStack();
}
if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
// 标准动画
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
// 自定义动画
// API LEVEL 11
// fragmentTransaction.setCustomAnimations(
// R.animator.fragment_slide_left_enter,
// R.animator.fragment_slide_right_exit);
// API LEVEL 13
fragmentTransaction.setCustomAnimations(
R.animator.fragment_left_enter,
R.animator.fragment_left_exit,
R.animator.fragment_pop_left_enter,
R.animator.fragment_pop_left_exit);
fragmentTransaction.add(R.id.container, mTextFragmentOne);
// 加入到BackStack中
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void removeFragment() {
if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mFragmentManager.popBackStack();
}
animator的文件定义
fragment_left_enter.xml
View Code
fragment_left_exit.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"0dp"
android:valueTo=
"100dp"
android:valueType=
"floatType"
android:propertyName=
"translationX"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"1.0"
android:valueTo=
"0.0"
android:valueType=
"floatType"
android:propertyName=
"alpha"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
</set>
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"100dp"
android:valueTo=
"0dp"
android:valueType=
"floatType"
android:propertyName=
"translationX"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"0.0"
android:valueTo=
"1.0"
android:valueType=
"floatType"
android:propertyName=
"alpha"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
</set>
fragment_pop_left_exit.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"0dp"
android:valueTo=
"-100dp"
android:valueType=
"floatType"
android:propertyName=
"translationX"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
<objectAnimator
android:interpolator=
"@android:interpolator/decelerate_quint"
android:valueFrom=
"1.0"
android:valueTo=
"0.0"
android:valueType=
"floatType"
android:propertyName=
"alpha"
android:duration=
"@android:integer/config_mediumAnimTime"
/>
</set>