AndroidViewAnimations开源框架实现

来源:互联网 发布:上证综合指数数据 编辑:程序博客网 时间:2024/05/18 03:40

1.这是一个开源的动画库,首先感谢作者的分享。

BaseViewAnimator.class

这个是所有动画对象的基类,主要用来设置动画的属性。

public abstract class BaseViewAnimator {public static final long DURATION = 1000;private AnimatorSet mAnimatorSet;// 存放动画对象的集合private long mDuration = DURATION;// 默认的持续时间{mAnimatorSet = new AnimatorSet();}/** * 子类中需要实现的方法 *  * @param target */protected abstract void prepare(View target);/** * 设置目标view *  * @param target * @return */public BaseViewAnimator setTarget(View target) {reset(target);prepare(target);return this;}/** * 开始动画 */public void animate() {start();}/** * 开始动画 */public void start() {mAnimatorSet.setDuration(mDuration);mAnimatorSet.start();}/** * 重置view上面的状态 *  * @param target */public void reset(View target) {ViewHelper.setAlpha(target, 1);ViewHelper.setScaleX(target, 1);ViewHelper.setScaleY(target, 1);ViewHelper.setTranslationX(target, 0);ViewHelper.setTranslationY(target, 0);ViewHelper.setRotation(target, 0);ViewHelper.setRotationY(target, 0);ViewHelper.setRotationX(target, 0);ViewHelper.setPivotX(target, target.getMeasuredWidth() / 2.0f);ViewHelper.setPivotY(target, target.getMeasuredHeight() / 2.0f);}/** * 设置延续时间 *  * @param duration * @return */public BaseViewAnimator setDuration(long duration) {mDuration = duration;return this;}/** * 设置开始的延迟时间 *  * @param delay * @return */public BaseViewAnimator setStartDelay(long delay) {getAnimatorAgent().setStartDelay(delay);return this;}public long getStartDelay() {return mAnimatorSet.getStartDelay();}/** * 添加监听器 *  * @param l * @return */public BaseViewAnimator addAnimatorListener(AnimatorListener l) {mAnimatorSet.addListener(l);return this;}public void cancel() {mAnimatorSet.cancel();}public boolean isRunning() {return mAnimatorSet.isRunning();}public boolean isStarted() {return mAnimatorSet.isStarted();}public void removeAnimatorListener(AnimatorListener l) {mAnimatorSet.removeListener(l);}public void removeAllListener() {mAnimatorSet.removeAllListeners();}/** * 设置间插器 *  * @param interpolator * @return */public BaseViewAnimator setInterpolator(Interpolator interpolator) {mAnimatorSet.setInterpolator(interpolator);return this;}public long getDuration() {return mDuration;}/** * 获得动画对象存放的集合 *  * @return */public AnimatorSet getAnimatorAgent() {return mAnimatorSet;}}


Techniques.class

这个类是个枚举类,枚举出了所有实现了得对话对象:

public enum Techniques {    DropOut(DropOutAnimator.class),    Landing(LandingAnimator.class),    TakingOff(TakingOffAnimator.class),    Flash(FlashAnimator.class),    Pulse(PulseAnimator.class),    RubberBand(RubberBandAnimator.class),    Shake(ShakeAnimator.class),    Swing(SwingAnimator.class),    Wobble(WobbleAnimator.class),    Bounce(BounceAnimator.class),    Tada(TadaAnimator.class),    StandUp(StandUpAnimator.class),    Wave(WaveAnimator.class),    Hinge(HingeAnimator.class),    RollIn(RollInAnimator.class),    RollOut(RollOutAnimator.class),    BounceIn(BounceInAnimator.class),    BounceInDown(BounceInDownAnimator.class),    BounceInLeft(BounceInLeftAnimator.class),    BounceInRight(BounceInRightAnimator.class),    BounceInUp(BounceInUpAnimator.class),    FadeIn(FadeInAnimator.class),    FadeInUp(FadeInUpAnimator.class),    FadeInDown(FadeInDownAnimator.class),    FadeInLeft(FadeInLeftAnimator.class),    FadeInRight(FadeInRightAnimator.class),    FadeOut(FadeOutAnimator.class),    FadeOutDown(FadeOutDownAnimator.class),    FadeOutLeft(FadeOutLeftAnimator.class),    FadeOutRight(FadeOutRightAnimator.class),    FadeOutUp(FadeOutUpAnimator.class),    FlipInX(FlipInXAnimator.class),    FlipOutX(FlipOutXAnimator.class),    FlipOutY(FlipOutYAnimator.class),    RotateIn(RotateInAnimator.class),    RotateInDownLeft(RotateInDownLeftAnimator.class),    RotateInDownRight(RotateInDownRightAnimator.class),    RotateInUpLeft(RotateInUpLeftAnimator.class),    RotateInUpRight(RotateInUpRightAnimator.class),    RotateOut(RotateOutAnimator.class),    RotateOutDownLeft(RotateOutDownLeftAnimator.class),    RotateOutDownRight(RotateOutDownRightAnimator.class),    RotateOutUpLeft(RotateOutUpLeftAnimator.class),    RotateOutUpRight(RotateOutUpRightAnimator.class),    SlideInLeft(SlideInLeftAnimator.class),    SlideInRight(SlideInRightAnimator.class),    SlideInUp(SlideInUpAnimator.class),    SlideInDown(SlideInDownAnimator.class),    SlideOutLeft(SlideOutLeftAnimator.class),    SlideOutRight(SlideOutRightAnimator.class),    SlideOutUp(SlideOutUpAnimator.class),    SlideOutDown(SlideOutDownAnimator.class),    ZoomIn(ZoomInAnimator.class),    ZoomInDown(ZoomInDownAnimator.class),    ZoomInLeft(ZoomInLeftAnimator.class),    ZoomInRight(ZoomInRightAnimator.class),    ZoomInUp(ZoomInUpAnimator.class),    ZoomOut(ZoomOutAnimator.class),    ZoomOutDown(ZoomOutDownAnimator.class),    ZoomOutLeft(ZoomOutLeftAnimator.class),    ZoomOutRight(ZoomOutRightAnimator.class),    ZoomOutUp(ZoomOutUpAnimator.class);    private Class animatorClazz;    private Techniques(Class clazz) {        animatorClazz = clazz;    }    public BaseViewAnimator getAnimator() {        try {            return (BaseViewAnimator) animatorClazz.newInstance();        } catch (Exception e) {            throw new Error("Can not init animatorClazz instance");        }    }}

YoYo.class

这个类采用了链式api的方式:

通过静态方法将动画对象传入:

 public static AnimationComposer with(Techniques techniques) {        return new AnimationComposer(techniques);    }    public static AnimationComposer with(BaseViewAnimator animator) {        return new AnimationComposer(animator);    }
  内部类 AnimationComposer.class就相当于一个builder构造器,用来设置各种属性:

 public static final class AnimationComposer {        private List<Animator.AnimatorListener> callbacks = new ArrayList<Animator.AnimatorListener>();        private BaseViewAnimator animator;        private long duration = DURATION;        private long delay = NO_DELAY;        private Interpolator interpolator;        private View target;        private AnimationComposer(Techniques techniques) {            this.animator = techniques.getAnimator();        }        private AnimationComposer(BaseViewAnimator animator) {            this.animator = animator;        }        public AnimationComposer duration(long duration) {            this.duration = duration;            return this;        }        public AnimationComposer delay(long delay) {            this.delay = delay;            return this;        }        public AnimationComposer interpolate(Interpolator interpolator) {            this.interpolator = interpolator;            return this;        }        public AnimationComposer withListener(Animator.AnimatorListener listener) {            callbacks.add(listener);            return this;        }        public YoYoString playOn(View target) {            this.target = target;            return new YoYoString(new YoYo(this).play(), this.target);        }    }
通过一系列方法设置一系列参数,最后调用

playOn(View target)

public YoYoString playOn(View target) {            this.target = target;            return new YoYoString(new YoYo(this).play(), this.target);        }
来开启动画,实际上最后还是调用了new YoYo(this).play()来开启动画:

private BaseViewAnimator play() {        animator.setTarget(target);        animator.setDuration(duration)                .setInterpolator(interpolator)                .setStartDelay(delay);        if (callbacks.size() > 0) {            for (Animator.AnimatorListener callback : callbacks) {                animator.addAnimatorListener(callback);            }        }        animator.animate();        return animator;    }

animator.animate();是调用如下方法:

/** * 开始动画 */public void animate() {start();}/** * 开始动画 */public void start() {mAnimatorSet.setDuration(mDuration);mAnimatorSet.start();}
然后各种动画效果就不分析了。



0 0
原创粉丝点击