ValueAnimator----ofObject

1.ofObject可以传入任何类型的变量,该函数的定义如下:

public static ValueAnimator ofObject(TypeEvaluator evaluator, Object ...values)

它的参数有两个,第一个是自定义的Evaluator;第二个则是object类型的可变长参数。


2.1透明操作:

ObjectAnimation animator = ObjectAnimation.ofFloat(tv,"alpha",1,0,1);

animator.setDuration(2000);

animator.start();

第一个参数是动画操作的控件,第二个参数是操作的属性,第三个参数是可变长参数,指定上述属性值如何变化。

2.2实现旋转效果:

ObjectAnimation animator = ObjectAnimator.ofFloat(tv,"rotation",0,180,0);

animator.setDuration(2000);

animator.start();


3.View 中已经实现了与alpha、rotation、translate、scale相关的set函数,所以在构造ObjectAnimation时可以直接使用
3.1要使用ObjectAnimator来构造动画,在要操作的控件中必须存在对应属性的set函数。而且参数类型必须和构造函数所使用的ofFloat或者ofInt相一致

3.2set函数的命名,set后每个单词首字母大写,其余字母小写


4.setRotationX(float rotationX) 围绕X轴旋转。。。度

setRotationY(float rotationY)围绕Y轴旋转。。。度

setRotation(float rotation)围绕Z轴旋转。。。度

setTranslationX(float translationX)表示在X轴上的平移距离,以当前控件为原点,向右为正方向,参数translationX表示移动的距离

setTranslationY(float translationY)

setScaleX(float scaleX)在X轴上进行缩放,scaleX表示缩放倍数

setScaleY(float scaleY)


5.ObjectAnimator动画原理

ValueAnimator动画流程

ofInt(0,400) 定义动画数值区间 ———> 插值器 返回当前进度 ——>Evaluator根据数值进度计算当前值——>监听器返回 在AnimaorUpdateListener中返回

ObjectAnimator动画流程

ofFloat(tv,"scale",0,3,1)定义动画对象及其区间;插值器 返回当前进度;Evaluator 根据数值进度计算当前值; 调用set函数 根据属性拼装set函数并反射调用,并将当前值作为参数传入

5.1拼接set函数的 方法,首先将属性的第一个字母大写,然后与set拼接,如果命名函数为setScalePoint

X(),那么属性可以写做scalePointX或者ScalePointX,第一个字母的大小写随意,但是后面部分必须和set函数后面部分大小写一致

5.2确定函数的参数类型


5.3

set函数就相当于我们在ValueAnimator中添加的监听器,set函数中对控件的操作还是需要自己来写


6.自定义ObjectAnimator属性

三个构造函数

public static ObjectAnimator ofFloat(Object target, String propertyName,float..values)

public static ObjectAnimator ofIt(Object target, String propertyName,int..values)

public static ObjectAnimator ofObject(Object target, String propertyName,object..values)

以实现抛物动画为例子:

如果使用ObjectAnimator,则需要操作控件中的某个属性,所以要从ImageView中派生一个类来表示圆球,在其中指定一个set函数以供ObjecctAnimator调用

public class FallingBaillImageView extends ImageView{

public FallingballImageView(Context context,AttributeSet attrs){

super(context,attrs);

}

public void setFallingPos(Point pos){

layout(pos.x,pos.y,pos.x+getWidth(),pos.y+getHeight());

}

}

这个set函数所对应的属性应该是fallingPos或者FallingPos

在setFallingPos()函数中,参数类型是Point对象,所以我们在构造ObjectAnimator时必须使用ofObject()函数。

主代码

ObjectAnimator animator = ObjectAnimator.ofObject(ball_img,"fallingPos",new FallingBallEvaluator(),new Point(0,0),new Point(500,500));

animator.setDuration(2000);

animator.start();

ObjectAnimator中,操作的控件对象是ball_img,对应的属性是fallingPos,值从点(0,0)运动到点(500,0,500)其中FallingBallEvaluator的实现如下

public class FallingBallEvaluator implements TypeEvaluator{

private Point point = new Point();

public Point evaluate(float fraction,Point startValue, Point endValue){

point.x=(int)(startValue.x+fraction*(endValue.x-startValue.x));

if(franction*2<=1){

point.y=(int)(startValue.y+fraction*2*(endValue.y-startValue.y));

}else{

point.y=endValue.y;

}

return point;}

}


6.1何时需要实现对应属性的get函数

仅当给动画设置一个值的时候,程序才会调用属性对应的get函数来得到动画初始值,如果动画没有初始值,便会使用系统默认值,比如ofInt()函数系统的默认值是0.

你可能感兴趣的:(ValueAnimator----ofObject)