这是我参与11月更文挑战的第24天,活动详情查看:2021最后一次更文挑战
前言
属性动画不同与View动画,属性动画是在API 11加入的新特性,也是为了补充View动画功能不够丰富的短板。属性动画不只作用于View对象,属性动画可以对任何对象做动画效果。另外属性动画加强了动画效果不单单局限于View动画四种简单效果,此外还有ValueAnimator、ObjectAnimator以及AnimatorSet等和动画相关功能类帮助开发者实现更高级动画功能。
属性动画
属性动画的作用对象是任何对象,不仅仅局限视图View对象。动画效果除了基本变换效果外还有其他动画效果可以实现。属性动画实现过程是在一定时间间隔内,通过改变属性值来改变对于属性值的属性,从而实现作用对象在该属性上的动画效果。
flowchart TB
开始 --> B[预设动画时长,动画效果属性值包括开始值和结束值]--> 设置动画变化属性插值器或估值器 --> 执行动画不断刷新视图绘制更新实现动画效果 -->结束
ValueAnimator
ValueAnimator是属性动画中重要部分,它实现动画原理是通过不断改变控制值然后赋值给对象属性来实现动画效果。
创建ValueAnimator并设置过渡值0-1,设置时长300ms,然后设置监听器,执行start后从监听器回调中实时获取到变换值。变换值在300ms内会从0一直增加到1,这个过程是由ValueAnimator内部计算得出的。同时ValueAnimator的过渡值可以设置多个,例如可以像是0-5-10-4-5不断过渡变化。除此之外ValueAnimator和View动画一样也有设置setRepeatCount()和setRepeatMode()方法就不多介绍。
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.setDuration(300);
//设置监听获取实时变化的数值
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float currentValue = (float) animation.getAnimatedValue();
}
});
anim.start();
ValueAnimator.ofFloat(0f, 5f,15f,4f,10f); // 可以设置多个过渡值
ObjectAnimator
ValueAnimator只是对值进行一个动画过渡效果,实质上就是将动画以值的形式呈现。ObjectAnimator则有所不同,它可以直接作用于对象的属性进行动画操作。例如View的透明底设置,ObjectAnimator的ofFloat方法第一个入参为Object对象,然后是属性值,最后才是过渡值参数。
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);
animator.setDuration(3000);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f,360f);
animator.setDuration(3000);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0f,-360f,0f);
animator.setDuration(3000);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "scaleX", 1f,2f,1f);
animator.setDuration(3000);
animator.start();
ObjectAnimator对于属性对象的设置是以字符串的形式。已知代码中所设置的值之外,如何知道哪些属性对象是可以被设置和使用的。由于ObjectAnimator并不是单纯为了View动画而设计,对于属性对象没有绝对界限,它只负责对某个属性值进行赋值,在找到对应属性对象然后去赋值变换。
举例修改TextView背景颜色,设置TextView背景颜色方法是setBackgroundColor,颜色参数值是Int类型因此可以采用ObjectAnimator.ofInt,属性对象的字符串设置是BackgroundColor。ObjectAnimator就能根据属性对象找到对应方法来修改参数实现改变背景颜色的效果。
TextView textView = new TextView(this);
textView.setText("属性动画");
textView.setBackgroundColor(Color.RED);
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(textView,"BackgroundColor",Color.RED,Color.BLUE);
objectAnimator.setDuration(10000);
objectAnimator.setRepeatCount(-1);
objectAnimator.start();
Animator监听器
Animator监听器有Animator.AnimatorListener和AnimatorListenerAdapter。AnimatorListenerAdapter解决了动画每个回调都需要实现的问题,按需回调希望使用的回调接口。
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
objectAnimator.addListener(new AnimatorListenerAdapter() {
});
总结
属性动画的xml文件配置形式和View动画基本是差不多的,相比之下使用代码实现会更加灵活可变性更高。属性动画比起View动画就是可以对任意对象进行动画操作,由于属性动画是Android3.0对于目前市面主流来说直接使用属性动画也不存在许多问题。另外属性动画关于插值器和估值器等更多高级开发之后可以继续展开。