Android属性动画

来源:互联网 发布:vc6.0连接sql数据库 编辑:程序博客网 时间:2024/06/05 15:32

属性动画

属性动画的基本原理是通过不断改变View的属性来改变View的位置透明度等特性,从而形成动画。
属性动画和其他动画的不同之处体现在属性动画是真实改变了View自身的属性。最为明显的例子是当使用Animation动画时,点击事件不会跟随着View的位置改变而改变,造成了View移动了,但是事件的位置还时原来的位置。而属性动画由于时真实改变了View的属性,而不仅仅是在屏幕上进行重绘,所以View的事件会跟随着View的移动而做相应变化。

属性动画的使用

属性动画中有valueAnimator, ObjectAnimator, AnimatorSet等概念。

其中ValueAnimator是产生动画的值,而本身并没有对View或者对象做一个动画的变化过程,需要开发者自己使用addUpdateListener添加一个更新值的接口回调并在其中去更新View或者对象。使用方法如下:

ValueAnimator valueAnimator = ValueAnimator.ofInt(0,500);valueAnimator.setDuration(5000);valueAnimator.setInterpolator(new LinearInterpolator());valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {      @Override      public void onAnimationUpdate(ValueAnimator animation) {      Log.e("ZXX", animation.getAnimatedValue()+"");      //在这里更新View的属性可实现动画      }});valueAnimator.start();

而ObjectAnimator是继承了ValueAnimator实现的,ObjectAnimator的使用十分方便,比如改变一个对象(myObject)的translationY属性,让其沿Y轴向上平移一段距离它的高度,实现如下:

ObjectAnimator.ofFloat("myObject", "translationY", -myObject.getHeight()).start();

其中不需要设置时常也可以,因为属性动画有默认时常为300ms,默认帧率10ms/帧。

Animator的使用

AnimatorSet animatorSet = new AnimatorSet();animatorset.playTogether(    ObjectAnimator.ofFloat("myObject", "translationY", -myObject.getHeight());    ObjectAnimator.ofFloat("myObject", "translationX", -myObject.getWidth());    ObjectAnimator.ofFloat("myObject", "alpha", 0.5););animatorset.setDuration(5 * 1000).start();

属性动画中还有两个重要的知识点是插值器和估值器,通过插值器和估值器可以产生加速动画,减速动画,先加速后减速等各种非线性的动画。
先讲估值器TypeEvaluator,直接看代码:

TypeEvaluator<Integer> typeEvaluator = new TypeEvaluator<Integer>() {    @Override    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {    int result = (int) (startValue + fraction * (endValue - startValue));    Log.e("ZXX","startValue: " + startValue);    Log.e("ZXX", "endValue: " + endValue);    Log.e("ZXX", "result: "+result);    return result;    }};

在实现一个估值器时,首先是泛型,传入影响动画的实体类,估值器中只有一个evaluate的方法,该方法的参数有fraction, startValue, endValue。其中,fraction表示当前时间占动画总时间的百分比,startValue表示动画的起始值,endValue表示动画的结束值。通过这三个值可以算出每个时刻动画的值,该值就是ValueAnimator中AnimatorUpdateListener通过getAnimatedValue()得到的值,然后根据值去实现动画。因此我们可以通过它来控制动画的过程。

而插值器Interpolator是控制上述估值器中的fraction的值的,如果是线性的那么直接返回input的值,如果是非线性,则根据非线性特性返回相应的值,可以实现加减速等动画。

Interpolator interpolator = new Interpolator() {    @Override    public float getInterpolation(float input) {        Log.e("ZXX","fraction: " + input);        return input;    }};

比如返回input则是线性,返回input * input则成为非线性。

例子:

ValueAnimator valueAnimator = ValueAnimator.ofInt(0,500);valueAnimator.setDuration(5000);//设置插值器valueAnimator.setInterpolator(interpolator);//设置估值器valueAnimator.setEvaluator(typeEvaluator);valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {        Log.e("ZXX", animation.getAnimatedValue()+"");    }});valueAnimator.start();

查看部分Log:
08-23 23:35:20.214 3506-3506/com.example.xin.animdemo E/ZXX: fraction: 0.0734
08-23 23:35:20.214 3506-3506/com.example.xin.animdemo E/ZXX: startValue: 0
08-23 23:35:20.214 3506-3506/com.example.xin.animdemo E/ZXX: endValue: 500
08-23 23:35:20.214 3506-3506/com.example.xin.animdemo E/ZXX: result: 36
08-23 23:35:20.214 3506-3506/com.example.xin.animdemo E/ZXX: 36
08-23 23:35:20.219 3506-3506/com.example.xin.animdemo E/ZXX: fraction: 0.0766
08-23 23:35:20.219 3506-3506/com.example.xin.animdemo E/ZXX: startValue: 0
08-23 23:35:20.219 3506-3506/com.example.xin.animdemo E/ZXX: endValue: 500
08-23 23:35:20.219 3506-3506/com.example.xin.animdemo E/ZXX: result: 38
08-23 23:35:20.219 3506-3506/com.example.xin.animdemo E/ZXX: 38
08-23 23:35:20.236 3506-3506/com.example.xin.animdemo E/ZXX: fraction: 0.08
08-23 23:35:20.237 3506-3506/com.example.xin.animdemo E/ZXX: startValue: 0
08-23 23:35:20.237 3506-3506/com.example.xin.animdemo E/ZXX: endValue: 500
08-23 23:35:20.237 3506-3506/com.example.xin.animdemo E/ZXX: result: 40
08-23 23:35:20.237 3506-3506/com.example.xin.animdemo E/ZXX: 40
08-23 23:35:20.252 3506-3506/com.example.xin.animdemo E/ZXX: fraction: 0.0834
08-23 23:35:20.252 3506-3506/com.example.xin.animdemo E/ZXX: startValue: 0
08-23 23:35:20.252 3506-3506/com.example.xin.animdemo E/ZXX: endValue: 500
08-23 23:35:20.253 3506-3506/com.example.xin.animdemo E/ZXX: result: 41
08-23 23:35:20.253 3506-3506/com.example.xin.animdemo E/ZXX: 41

startValue + fraction * (endValue - startValue)便是计算各个时刻的动画值的公式

原创粉丝点击