补间动画并不能改变view的真实坐标 属性动画可以改变view的真实坐标
ValueAnimator animator = ValueAnimator.ofInt(0,400);
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int curValue = (int)animation.getAnimatedValue();
Log.d("qgl","curValue:"+curValue);
}
});
animator.start();
ValueAnimator只负责对指定的数字区间进行动画运算
我们需要对运算过程进行监听,然后自己对控件做动画
ValueAnimator animator = ValueAnimator.ofFloat(0f,400f,50f,300f);
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float curValueFloat = (Float)animation.getAnimatedValue();
int curValue = curValueFloat.intValue();
}
});
animator.start();
清除动画的时候要清除动画的监听事件 然后调用cancel方法
/**
* 移除AnimatorUpdateListener
*/
void removeUpdateListener(AnimatorUpdateListener listener);
void removeAllUpdateListeners();
/**
* 移除AnimatorListener
*/
void removeListener(AnimatorListener listener);
void removeAllListeners();
/**
* 调用cancel方法
*/
valueAnimator.cancel();
Evaluator
这幅图讲述了定义动画的数字区间到通过AnimatorUpdataListener中得到当前动画所对应数值的整个过程,具体理解如下:
1、ofInt(0,400)表示指定动画的数字区间,是从0运动到400
2、加速器:在动画开始后,通过加速器会返回当前动画进度所对应的数字进度,但这个数字进度是百分制的,以小数表示,如0.2
3、Evaluator:我们知道我们通过监听器拿到的是当前动画所对应的具体的数值,而不是百分制的进度。那么就必须有一个地方会根据当前的数字进度,将其转换成对应的数值,这个地方就是Evaluator;Evaluator就是将从加速器返回的数字进度转换成对应的具体的数值。
4、监听器:我们通过在AnimatorUpdateListener监听器使用animator.getAnimatedValue()方法拿到的就是Evaluator中返回的数值
其实Evaluator就是一个转换器,他能把小数进度转换成对应的具体的数值
ofInt默认对应IntEvaluator onFloat默认对应FloatEvaluator
IntEvaluator的源码如下:
/**
* This evaluator can be used to perform type interpolation between <code>int</code> values.
*/
public class IntEvaluator implements TypeEvaluator<Integer> {
/**
* This function returns the result of linearly interpolating the start and end values, with
* <code>fraction</code> representing the proportion between the start and end values. The
* calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>,
* where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
* and <code>t</code> is <code>fraction</code>.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue The start value; should be of type <code>int</code> or
* <code>Integer</code>
* @param endValue The end value; should be of type <code>int</code> or <code>Integer</code>
* @return A linear interpolation between the start and end values, given the
* <code>fraction</code> parameter.
*/
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(startInt + fraction * (endValue - startInt));
}
}
IntEvaluator中只有一个函数(float fraction,Integer startValue,Integer endValue);
其中fraction就是加速器中返回的值,表示当前动画的数值进度,百分制的小数表示。
startValue和endValue分别对应ofInt(int start,int end)中的start和end的数值
假设当我们定义的动画的ofInt(100,400)进行到数值进度20%的时候,那么此时在evaluate方法中,fraction的值就是0.2,startValue的值是100,endValue的值是400,根据evaluate方法里的计算公式 当前的值 = 100 + (400 - 100) * 0.2;
我们可以通过重写加速器改变数值进度来改变具体返回的数值,也可以通过改变Evaluator中进度所对应的数值来改变具体返回的数值!!!
ArgbEvalutor
源码如下:
public class ArgbEvaluator implements TypeEvaluator {
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24);
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = (Integer) endValue;
int endA = (endInt >> 24);
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;
return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
(int)((startR + (int)(fraction * (endR - startR))) << 16) |
(int)((startG + (int)(fraction * (endG - startG))) << 8) |
(int)((startB + (int)(fraction * (endB - startB))));
}
}
我们在这里关注两个地方:
第一:返回值是int类型,这说明我们可以使用ofInt()来初始化动画数值范围。
第二:颜色值包括A,R,G,B四个值,每个值是8位 所以用16进制表示一个颜色值应该是0xffff0000(纯红色)。
使用示例如下:
ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff);
animator.setEvaluator(new ArgbEvaluator());
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int curValue = (int)animation.getAnimatedValue();
tv.setBackgroundColor(curValue);
}
});
animator.start();