Android动画那些事儿---属性动画(Property Animation)

本文同步发布在掘金,转载请注明出处。

上篇文章详细讲解了视图动画,也提到了视图动画存在的先天不足,即补间动画不具有交互性。动画改变的只是显示效果,其响应事件却依然还在原来的位置。在Android3.0之后引入了属性动画,属性动画相比视图动画具有更强大的功能,不仅弥补了视图动画的先天不足,而且属性动画不仅仅可以作用于View,而是作用于任意对象。接下来,我们依然结合Android动画的结构图来看:
在这里插入图片描述
从图中可以看到,属性动画有ValueAnimator和AnimatorSet两个类,这两个类均继承自android.animation.Animator类,从名字上来看ValueAnimator叫数值动画,是一个动画类,它有两个子类分别是ObjectAnimator和TimeAnimator;而AnimatorSet从名字上也可以看得出来它是一个属性动画的集合类。因此我们就自上而下从Animator这个类开始逐步了解Android的属性动画。

1.Animator类

Animator类是一个抽象类,是ValueAnimator和AnimatorSet的父类,因此这个方法中一定声明了许多公共的方法:

Method Method description
void start() Starts this animation.
void cancel() Cancels the animation.
void end() Ends the animation.
void pause() Pauses a running animation.
void resume() Resumes a paused animation
void addListener(AnimatorListener listener) Adds a listener to the set of listeners that are sent events through the life of ananimation, such as start, repeat, and end.
setInterpolator(TimeInterpolator value) The time interpolator used in calculating the elapsed fraction of theanimation.

Animator中常用到的方法大概也就这么多,因为Animator是一个抽象类,因此这个类中的方法大多是抽象方法或者是空方法,具体的实现都在子类中,并且从方法的名字上就能看懂方法的用途,因此关于这个类没什么要说的,不过最后一个方法setInterpolator(TimeInterpolator value)可能会有些陌生,这个方法是为动画设置一个插值器,可以去控制动画的速率,关于插值器的知识暂且了解就好,后续会对插值器深入讲解。

2.ValueAnimator

ValueAnimator在属性动画中有着非常重要的作用,它是属性动画的核心。ValueAnimator本身并没有提供任何动画效果,它会接收一个初始值和结束值,在运行的过程中不断的改变数值的大小从而实现动画的变换。我们来看ValueAnimator中提供的API:

Method Method description
ValueAnimator ofInt(int… values) Constructs and returns a ValueAnimator that animates between int values.
ValueAnimator ofFloat(float… values) Constructs and returns a ValueAnimator that animates between float values.
ValueAnimator ofArgb(int… values) Constructs and returns a ValueAnimator that animates between color values.
ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder… values) Constructs and returns a ValueAnimator that animates between the values specified in the PropertyValuesHolder objects.
ValueAnimator ofObject(TypeEvaluator evaluator, Object… values) Constructs and returns a ValueAnimator that animates between Object values.
void setRepeatCount(int value) Sets how many times the animation should be repeated.
setRepeatMode(@RepeatMode int value) Defines what this animation should do when it reaches the end.
addUpdateListener(AnimatorUpdateListener listener) Adds a listener to the set of listeners that are sent update events through the life of an animation.

上面我们列出了ValueAnimator中常用的一些方法,前面我们提到ValueAnimator其实是一个数值生成器,那么ValueAnimator是如何产生一个变化的数值的呢?下面看一个例子:

ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1);
        valueAnimator.setDuration(200);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                Log.e("ValueAnimator","animatedValue-------"+animatedValue);
            }
        });
        valueAnimator.start();

上述代码初始化了一个从0-1的数值是float类型的ValueAnimator,我们为其设置了200毫秒的动画时间,并为其添加了addUpdateListener来监听动画更新,并将日志打印出来:
在这里插入图片描述
可以看到在动画的执行过程中控制台打印除了一系列个从0-1平滑过渡的浮点数值。有些小伙伴可能注意到ofFloag方法接收的是一个可变长度的参数,如果我们传入多个参数会怎样呢?可以试下如下代码:

ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1,0);
        valueAnimator.setDuration(200);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
              
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值