Skip to content

自定义进入动画

redrain39 edited this page Oct 24, 2019 · 1 revision

在 GiraffeHero 中提供了自定义进入动画的方法。

继承进入动画基类 BaseInAnimation 实现 getAnimators 方法,将自定义的组合动画集写入到该方法中,在需要使用自定义进入动画的 Provider 中的 getAnimator() 中加入对应的自定义动画即可使用。

此为 GiraffeHero 所提供的 SlideInAnimation 实例

// 继承BaseInAnimation基类
public class SlideInAnimation extends BaseInAnimation {

    private SlideDirection mSlideDirection;

    public SlideInAnimation() {
        this.mSlideDirection = SlideDirection.RIGHT;
    }

    public SlideInAnimation(SlideDirection slideDirection) {
        this.mSlideDirection = slideDirection;
    }

    // 实现此方法,将所需要的组合动画集写入
    @Override
    public AnimatorSet getAnimators(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setStartDelay(getStartDelay());
        animatorSet.setDuration(getDuration());
        animatorSet.setInterpolator(getInterpolator());
        ObjectAnimator animator = null;
        switch (mSlideDirection) {
            case TOP:
                view.setTranslationY(-view.getRootView().getHeight());
                animator = ObjectAnimator.ofFloat(view, "translationY", -view.getRootView().getHeight(), 0);
                break;
            case BOTTOM:
                view.setTranslationY(view.getRootView().getHeight());
                animator = ObjectAnimator.ofFloat(view, "translationY", view.getRootView().getHeight(), 0);
                break;
            case LEFT:
                view.setTranslationX(-view.getRootView().getWidth());
                animator = ObjectAnimator.ofFloat(view, "translationX", -view.getRootView().getWidth(), 0);
                break;
            case RIGHT:
                view.setTranslationX(view.getRootView().getWidth());
                animator = ObjectAnimator.ofFloat(view, "translationX", view.getRootView().getWidth(), 0);
                break;
        }

        animatorSet.play(animator);
        // 返回一个动画集合
        return animatorSet;
    }

}
Clone this wiki locally