你可以给任意对象定义一个Animation并使之移动,不管这个对象是否绘制在屏幕上。可以通过指定希望发生动画的属性的改变值及持续时间来完成Animation。
一个Property Animation可以指定这些属性:
Duration:指定动画的持续时间,默认时间长度为300毫秒。
Time interpolation:指定插入函数,即属性和当前时间之间的函数。
Repeat count and behavior:可以指定动画是否重复播放以及重复播放的次数,还可以指定播放完一次后是否倒序(reverse 播放。
Animation sets:可以指定逻辑上的一组动画,他们可以一起播放,也可以一个接一个的播放。
Frame refresh delay:可以指定每帧的刷新频率,默认是10ms刷新一次,当然具体的还受系统的当前情况影响。
如何使用Property Animator:
其中ValueAnimator封装了一个TimeInterpolator,TimeInterpolator代表动画的加速曲线函数,TypeEvaluator表示TimeIntrepolator计算的数据类型,如IntEvaluator。
要开始一个动画,需要定义好属性的初值和终值以及持续时间,除此之外还有TimeInterpolator,最后调用start()方法。Property Animation和View Animation的不同之处,View Animation只能作用在View上,View Animation在实现上也有限制,他只支持诸如缩放,旋转之类的动画。另外,View Animation其实只是改变了View的draw效果,其实际的焦点并没有改变。
View上的ValueAnimatorAnimator
ValueAnimatorAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
fadeAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
balls.remove(((ObjectAnimator)animation).getTarget());
}
指定关键帧
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(5000ms);
View上面的ObjectAnimator
ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
载入xml动画
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);
set.start();