美文网首页Android
Android 过渡动画

Android 过渡动画

作者: yuzhiyi_宇 | 来源:发表于2018-09-22 16:56 被阅读40次

过渡动画(Transition Animation)是在 Android 4.4 引入的新的动画框架,它本质上还是属性动画,只不过是对属性动画做了一层封装,方便开发者实现 Activity 或者 View 的过渡动画效果。和属性动画相比,过渡动画最大的不同是需要为动前后准备不同的布局,并通过对应的 API 实现两个布局的过渡动画,而属性动画只需要一个布局文件。
几个基本概念。

  • Scene: 定义了页面的当前状态信息,Scene 的实例化一般通过静态工厂方法实现。
public static Scene getSceneForLayout(ViewGroup sceneRoot, int layoutId, Context context) {
  • Transition: 定义了界面之间切换的动画信息,在使用 TransitionManager 时没有指定使用哪个 Transition,那么会使用默认的 AutoTransition。
public class AutoTransition extends TransitionSet {

    /**
     * Constructs an AutoTransition object, which is a TransitionSet which
     * first fades out disappearing targets, then moves and resizes existing
     * targets, and finally fades in appearing targets.
     *
     */
    public AutoTransition() {
        init();
    }

    public AutoTransition(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOrdering(ORDERING_SEQUENTIAL);
        addTransition(new Fade(Fade.OUT)).
                addTransition(new ChangeBounds()).
                addTransition(new Fade(Fade.IN));
    }
}
  • TransitionManager: 控制 Scene 之间切换的控制器,切换常用的方法有以下两个。
   /**
     * Convenience method to simply change to the given scene using
     * the default transition for TransitionManager.
     *
     * @param scene The Scene to change to
     */
    public static void go(Scene scene) {
        changeScene(scene, sDefaultTransition);
    }
   /**
     * Convenience method to simply change to the given scene using
     * the given transition.
     *
     * <p>Passing in <code>null</code> for the transition parameter will
     * result in the scene changing without any transition running, and is
     * equivalent to calling {@link Scene#exit()} on the scene root's
     * current scene, followed by {@link Scene#enter()} on the scene
     * specified by the <code>scene</code> parameter.</p>
     *
     * @param scene The Scene to change to
     * @param transition The transition to use for this scene change. A
     * value of null causes the scene change to happen with no transition.
     */
    public static void go(Scene scene, Transition transition) {
        changeScene(scene, transition);
    }

过渡动画使用很简单,首先定义同一个页面的两个布局,分别是动画前的布局和动画后的布局。

XML 资源文件方式

过渡动画的 XML 文件需要放在 res/transition 目录中。
使用代码加载。

ViewGroup viewGroup = findViewById(R.id.container);
TransitionInflater transitionInflater = TransitionInflater.from(this);
transitionInflater.inflateTransition(R.transition.transition_manager, container);    

代码方式

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
void goToScene(Scene scene) {
    ChangeBounds changeBounds = new ChangeBounds();
    changeBounds.setDuration(2000);
    Fade fadeOut = new Fade(Fade.OUT);
    fadeOut.setDuration(2000);
    Fade fadeIn = new Fade(Fade.IN);
    fadeIn.setDuration(2000);
    TransitionSet transitionSet = new TransitionSet();
    transitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
    transitionSet.addTransition(fadeOut)
      .addTransition(changeBounds)
      .addTransition(fadeIn);
    TransitionManager.go(scene, transitionSet);
}

相关文章

网友评论

    本文标题:Android 过渡动画

    本文链接:https://www.haomeiwen.com/subject/hakxoftx.html