Android 属性动画使用(三)

来源:互联网 发布:神之浩劫ps4港服网络 编辑:程序博客网 时间:2024/05/21 03:28

首先扯点别的:今天看了看十月一回家的火车票,只能说艰难,只剩下一张无座的票了,而且还是从下午一点到次日凌晨4点的慢车,太漫长了。难道临沂到上海就不能修一条高铁吗!看样这次得坐汽车了。

继续总结 Android 属性动画的使用

经过前面的学习,我总结了,无论是使用ValueAnimator还是ObjectAnimator,它们之所以能够完成从初始状态到结束状态的过渡,是因为必须有一个继承自TypeEvaluator的一个evaluator。ofInt方法是内置的IntEvaluator,ofFloat方法是内置的FloatEvaluator,而我们使用ofObject方法的时候就得自定义一个继承自TypeEvaluator来完成动画的过渡。

这就好坐火车从菏泽(起点)去上海(终点),TypeEvaluator就好比是火车,可以让你在规定时间内到达上海。但是这个火车是怎么跑的就不知道了,它可能在一个站停的时间长一点,在另外一个站停的时间短一点。那么是什么控制火车的这些行为呢?就是接下来说的Interpolator。

Interpolator(翻译过来是差值器,可以把它想象为汽车上的档位,挂不同的档速度是不一样的,但是必须得保证在规定的时间内跑到终点)

不同类型的差值器都间接实现TimeInterpolator接口,这个接口的定义是这样的:

//一个时间差值器决定动画的变化速率。使得动画可以做非线性运动,例如加速或者减速。A time interpolator defines the rate of change of an animation. This allows animations to have non-linear motion, such as acceleration and deceleration.

一些系统定义好的Interpolator,系统默认的差值器类型

 // The time interpolator to be used if none is set on the animation    private static final TimeInterpolator sDefaultInterpolator =            new AccelerateDecelerateInterpolator();

举个例子来看看下面的差值器的效果,我想改变button的translationY属性,让button向下移动1200px

 tY = button.getTranslationY(); o = ObjectAnimator.ofFloat(button, "translationY", tY, 1200); //不断更换差值器的类型 o.setInterpolator(new AccelerateInterpolator()); o.setDuration(2000); o.start();
//系统默认的方式,动画在开始和结束的时候变化很慢,在中间阶段变化很快AccelerateDecelerateInterpolator    An interpolator where the rate of change starts and ends slowly but accelerates through the middle. //动画在开始的时候变化很慢,然后做加速运动AccelerateInterpolator  An interpolator where the rate of change starts out slowly and and then accelerates. //button会先向上移动一段距离,然后加速向下移动1200pxAnticipateInterpolator  An interpolator where the change starts backward then flings forward. //button会先向上移动一段距离,然后加速向下,超过1200px的位置,然后最终停在1200px的位置AnticipateOvershootInterpolator An interpolator where the change starts backward then flings forward and overshoots the target value and finally goes back to the final value. //button 加速向下到达终点,然后弹起,落下,弹起,落下,最后停在终点BounceInterpolator  An interpolator where the change bounces at the end. //o.setInterpolator(new CycleInterpolator(2));button向下,向上(超过初始位置),向下,再向上,然后回到原来的位置。CycleInterpolator   Repeats the animation for a specified number of cycles. //开始的时候变化很快,先后减速到达终点DecelerateInterpolator  An interpolator where the rate of change starts out quickly and and then decelerates. //加速很快,并一直保持加速直到终点FastOutLinearInInterpolator Interpolator corresponding to fast_out_linear_in. //加速很快,但是减速很慢FastOutSlowInInterpolator   Interpolator corresponding to fast_out_slow_in. //匀速直线运动LinearInterpolator  An interpolator where the rate of change is constant  //从最快的峰值速度逐渐减慢LinearOutSlowInInterpolator Interpolator corresponding to linear_out_slow_in.//加速超过终点,然后回到终点OvershootInterpolator   An interpolator where the change flings forward and overshoots the last value then comes back. //这个没有试PathInterpolator    An interpolator that can traverse a Path that extends from Point (0, 0) to (1, 1). 

如果你想要实现自己的动画速率变换方式,可以自定义差值器

public class MyTimeInterpolator implements TimeInterpolator {    @Override    public float getInterpolation(float input) {        float result;        //intput值[0,1]        if (input <= 0.5) {            result = (float) (Math.sin(Math.PI * input)) / 2;        } else {            result = (float) (2 - Math.sin(Math.PI * input)) / 2;        }        return result;    }}
//使用自定义的差值器anim.setInterpolator(new MyTimeInterpolator ());  

ViewPropertyAnimator的用法(专为View提供的快捷方便的属性动画)

1 改变透明度

 button.animate().alpha(0).setDuration(2000).start();

2 组合动画

 button1.animate()                        .scaleX(0.5f)                        .alpha(0.5f)                        .setDuration(3000)                        .setInterpolator(new LinearInterpolator()).start();

参考文章
【1】http://blog.csdn.net/guolin_blog/article/details/44171115
【2】https://developer.android.com/reference/android/animation/TimeInterpolator.html
【3】https://developer.android.com/reference/android/R.interpolator.html

结尾:该吃中午饭了。昨天发现一个很有趣的事情,我经常在家里说‘艰难’两个字,结果跟我合租房子的一个姐也学会了说这两个字,哈哈,当时就感觉到英吹思婷了!

0 0
原创粉丝点击