一、ObjectAnimator
重要的方法:
public static ObjectAnimator ofFloat(Objecttarget, String propertyName, float... values);
构造并返回一个ObjectAnimator对象。
注意values值的意义:
1.如果values只有一个值,表示动画的结束值;
2.如果有两个值,意味着一个起始值(starting)和一个结束值(ending)
3.如果多于两个值,第一个起始值(starting),最后一个结束值(ending),中间的会均匀的分配在动画的执行期的时间点上。
@param target 要设置动画的目标对象
@param propertyName 动画所针对的属性名字
@param values 动画随着时间推移,在执行的一些时间点上的值集合
@return构造的ObjectAnimator对象.public ObjectAnimator setDuration (longduration)
设置动画的时序时间,单位是milliseconds(毫秒).
public voidaddUpdateListener(ValueAnimator.AnimatorUpdateListener listener);
添加对动画的每一帧(frame)的变化的监听,监听方法将会在当前帧(frame)的value值被计算出来以后调用。
public void addListener(Animator.AnimatorListener listener);
添加对动画的生命周期各个阶段的监听,如start,repeat,end,cancel.如果只想关注结束的工作,则可以使用系统实现的一个listener,即:android.animation.AnimatorListenerAdapter,在这里面可以只覆盖onAnimationEnd()方法。
举例:
//设置view绕X轴翻转,动画效果从0度到360度.
ObjectAnimator anim =ObjectAnimator.ofFloat(view, "rotationX", 0F, 360.0F)//
.setDuration(500);
anim.start();
二、ValueAnimator
setTarget(Object target)
设置目标对象,表示动画是根据该目标对象的属性来运行的.
public void setEvaluator(TypeEvaluatorvalue)
设置Typeevaluator(类型监视器),在计算动画的属性值期间,会执行下面的方法:
public T evaluate(float fraction, TstartValue, T endValue)
参数 fraction表示从startValue到endValue变化的比例,值从0.0~1.0
startValue:起始点的值
endValue:结束点的值
返回值T:变化过程中的某个点的泛型值
举例:
垂直向下运动
ValueAnimator animator =ValueAnimator.ofFloat(0, mScreenHeight- mBlueBall.getHeight());
//animator.setTarget(mBlueBall);
animator.setDuration(1000).start();
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
@Override
publicvoid onAnimationUpdate(ValueAnimator animation) {
mBlueBall.setTranslationY((Float)animation.getAnimatedValue());
}
});抛物线运动:
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0,0));
valueAnimator.setInterpolator(newLinearInterpolator());
valueAnimator.setEvaluator(newTypeEvaluator<PointF>(){
@Override
publicPointF evaluate(float fraction, PointF startValue,
PointFendValue){
PointFpoint = new PointF();
point.x= 200 * fraction * 3;
point.y= 0.5f * 200 * (fraction * 3) * (fraction * 3);
returnpoint;
}
});
valueAnimator.start();
valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimatoranimation){
PointF point = (PointF)animation.getAnimatedValue();
mBlueBall.setX(point.x);
mBlueBall.setY(point.y);
}
});
三、AnimatorSet
public void playTogether (Animator...items);
设置AnimatorSet使anim1和anim2同时执行
//将图片mBlueBall沿着X和Y轴两个方向同时放大
ObjectAnimator anim1 =ObjectAnimator.ofFloat(mBlueBall, "scaleX",1.0f, 2f);
ObjectAnimator anim2 =ObjectAnimator.ofFloat(mBlueBall, "scaleY",1.0f, 2f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(2000);
animSet.setInterpolator(newLinearInterpolator());
//两个动画同时执行
animSet.playTogether(anim1, anim2);
animSet.start();
play(anim1).with(anim2)表示设置AnimatorSet同时播放anim1和anim2;
play(anim1).before(anim2)表示设置AnimatorSet先播放anim1,然后紧接着播放anim2;
play(anim1).after(anim2)表示设置AnimatorSet先播放anim2,然后紧接着播放anim1;
举例:
float cx = mBlueBall.getX();
ObjectAnimator anim1 =ObjectAnimator.ofFloat(mBlueBall, "scaleX",1.0f, 2f);
ObjectAnimator anim2 =ObjectAnimator.ofFloat(mBlueBall, "scaleY",1.0f, 2f);
ObjectAnimator anim3 =ObjectAnimator.ofFloat(mBlueBall,"x", cx , 0f);
ObjectAnimator anim4 =ObjectAnimator.ofFloat(mBlueBall,"x", cx);
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim1).with(anim2);
animSet.play(anim2).with(anim3);
animSet.play(anim4).after(anim3);
animSet.setDuration(1000);
animSet.start();