在Activity跳转之间可以设置动画效果,例如平移,渐变,旋转等动画,当然在Fragment中也可以设置切换的动画效果,可以达到跟ViewPager切换动画类似的效果。在Fragment中设置自定义切换动画主要分为了两种情景,一种是使用 android.app.Fragment包里面的Fragment类时,FragmentManager里面的FragmentTransaction事务只支持XML中的animator标签,如objectAnimator属性动画,不支持传统的补间动画标签如
另一种是使用兼容性版本的android.support.v4.app.Fragment包里面的Fragment类,SupportFragmentManager里面的FragmentTransaction事务支持XML中的animation标签,也就是传统的补间动画,例如
设置Fragment的切换动画主要是使用FragmentTransaction事务类的中setCustomAnimations方法:
setCustomAnimations
FragmentTransaction setCustomAnimations (int enter,
int exit,
int popEnter,
int popExit)
该方法有4个参数,第一个参数enter是指当一个Fragment被添加added 或者绑定attached到视图上,该Fragment进入视图时的animation或者animator的资源ID;第二个参数exit是指当一个Fragment从视图上被移除removed或者解除绑定detached时,该Fragment移出视图时的animation或者animator的资源ID;第三个参数popEnter是指当调用popBackStack()方法或者类似的方法弹出栈顶的Fragment后,Fragment栈的栈顶Fragment重新被添加readded或重新绑定reattached到视图上时,该Fragment进入视图时的animation或者animator的资源ID;第四个参数popExit是指当调用popBackStack()方法或者类似的方法弹出栈顶的Fragment时,该弹出的Fragment即从视图上被移除removed或者解除绑定detached,该Fragment移出视图时的animation或者animator的资源ID。
要实现Fragment的自定义切换动画效果,首先需要自定义XML动画资源文件,可以在res文件夹下新建anim文件夹然后新建XML资源文件。由于android.support.v4.app.Fragment包只支持补间动画的标签,所以可以按照自己的想法来在根标签set中新建动画标签来实现效果,如translate平移,alpha渐变透明度,rotate旋转,scale缩放等。
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0%p"
android:duration="@integer/card_flip_time_full"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="@integer/card_flip_time_full"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="@integer/card_flip_time_full"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="@integer/card_flip_time_full"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="@integer/card_flip_time_full"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="@integer/card_flip_time_full"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:duration="@integer/card_flip_time_full"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="@integer/card_flip_time_full"/>
set>
public class MainActivity extends BaseActivity {
private FirstFragment firstFragment;
private SecondFragment secondFragment;
private Button previous;
private Button next;
@Override
protected void initView() {
setContentView(R.layout.activity_main);
previous= (Button) findViewById(R.id.Btn_Previous);
next= (Button) findViewById(R.id.Btn_Next);
}
@Override
protected void initData() {
firstFragment=new FirstFragment();
secondFragment=new SecondFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.Container,firstFragment)
.commit();
}
@Override
protected void initListener() {
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager()
.popBackStack();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(
R.anim.slide_right_in,
R.anim.slide_left_out,
R.anim.slide_left_in,
R.anim.slide_right_out
).replace(R.id.Container,secondFragment)
.addToBackStack(null)
.commit();
}
});
}
}
android.app.Fragment包里面的FragmentManager的Transaction事务只支持属性动画的资源标签,所以可以在res文件夹中新建animator文件夹,然后在该animator中的根标签set内新建
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotationY"
android:valueFrom="-180"
android:valueTo="0"
android:duration="@integer/card_flip_time_full"
android:interpolator="@android:interpolator/accelerate_decelerate"/>
<objectAnimator
android:propertyName="alpha"
android:valueFrom="0.0"
android:valueTo="1.0"
android:duration="1"
android:startOffset="@integer/card_flip_time_half"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="-180"
android:duration="@integer/card_flip_time_full"
android:interpolator="@android:interpolator/accelerate_decelerate"/>
<objectAnimator
android:propertyName="alpha"
android:valueFrom="1.0"
android:valueTo="0.0"
android:duration="1"
android:startOffset="@integer/card_flip_time_half"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotationY"
android:valueFrom="180"
android:valueTo="0"
android:duration="@integer/card_flip_time_full"
android:interpolator="@android:interpolator/accelerate_decelerate"/>
<objectAnimator
android:propertyName="alpha"
android:valueFrom="0.0"
android:valueTo="1.0"
android:duration="1"
android:startOffset="@integer/card_flip_time_half"/>
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="180"
android:duration="@integer/card_flip_time_full"
android:interpolator="@android:interpolator/accelerate_decelerate"/>
<objectAnimator
android:propertyName="alpha"
android:valueFrom="1.0"
android:valueTo="0.0"
android:duration="1"
android:startOffset="@integer/card_flip_time_half"/>
set>
public class MainActivity extends BaseActivity {
private FirstFragment firstFragment;
private SecondFragment secondFragment;
private Button previous;
private Button next;
@Override
protected void initView() {
setContentView(R.layout.activity_main);
previous= (Button) findViewById(R.id.Btn_Previous);
next= (Button) findViewById(R.id.Btn_Next);
}
@Override
protected void initData() {
firstFragment=new FirstFragment();
secondFragment=new SecondFragment();
getFragmentManager()
.beginTransaction()
.add(R.id.Container,firstFragment)
.commit();
}
@Override
protected void initListener() {
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager()
.popBackStack();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in,
R.animator.card_flip_left_out,
R.animator.card_flip_left_in,
R.animator.card_flip_right_out
).replace(R.id.Container,secondFragment)
.addToBackStack(null)
.commit();
}
});
}
}