Skip to content

转场动画

JingYeoh edited this page Dec 18, 2017 · 2 revisions

我们在使用Fragment的时候,默认是没有转场动画的,但是在某些场景下,我们不得不使用转场动画,原生的Fragment也为此提供了Api支持,但是写起来略微有些麻烦,所以本框架对此进行了简化。

支持场景:

  • 入栈
  • 出栈
  • show
  • replace
  • hide
  • remove
  • 自定义Animation

默认只能通过定义的xml动画文件来使用转场动画,如果需要使用更加复杂的动画,可能会使用继承Animation类的动画类,如有需要,请参考ContainerFragment.java,这里有两种动画的使用方式。

如何使用

在需要支持的Fragment中添加注解@Animator。该注解有四个参数:enter/exit/popEnter/popExit分别对应自身进入动画/自身退出动画/栈顶元素进入动画/栈顶元素退出动画自定义Xml动画

@Animator(enter = R.anim.push_left_in_no_alpha, exit = R.anim.push_right_out_no_alpha,
    popEnter = R.anim.push_right_in_no_alpha, popExit = R.anim.push_left_out_no_alpha)
@Puppet
public class StartFragment extend Fragment

自定义Animation动画

@Puppet
public class StartFragment extend Fragment{
  @Override
  public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    //如果动画太复杂,需要使用具体的动画类,请参考demo中的该方法,否则请勿重写该方法
    if (customAnim) return super.onCreateAnimation(transit, enter, nextAnim);
    if (enter) {
      return AnimationHelper.createRotate3dEnterAnimation();
    } else {
      return AnimationHelper.createRotate3dExitAnimation();
    }
  }
}