Property Animation就是通过动画的方式改变对象的属性。
基本用法
ValueAnimator是整个属性动画机制当中最核心的一个类,它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,我们只需要将初始值和结束值提供给ValueAnimator,并且告诉它动画所需运行的时长,那么ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果。除此之外,ValueAnimator还负责管理动画的播放次数、播放模式、以及对动画设置监听器等。
ValueAnimator的用法:将值0过渡到20,变换时长为5秒,可以通过addUpdateListener()方法 添加监听来获取动画更新时值发生的变化,然后可以根据该值进行一些其他操作。
1
2
3
4
5
6
7
8
9
10ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 20);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int currentValue = (int) valueAnimator.getAnimatedValue();
Log.d(TAG, currentValue + "");
}
});
valueAnimator.start();ofInt()
表示变化的是int值,offFloat()
表示变化的是float值,这两个方法里可以传入很多个参数来实现你想要进行的变化,比如从0到20再到0,可以写成1
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 20,0);
ObjectAnimator是继承自ValueAnimator的一个类,所以ValueAnimator的所有用法也适用于ObjectAnimator,ObjectAnimator可以对任意对象进行动画操作。
在这里可以使用
ofFloat(Object target, String propertyName, float... values)
方法对一个对象进行动画操作,同样也有相同参数的ofInt()方法来对传入对象的int值进行变化。以下是旋转动画,其中
ofFloat()
方法的第二个参数是对传入的对象的哪个属性进行动画操作,理论上,该值可以为传入对象的任意属性,因为ObjectAnimator在设计的时候就不是针对View对象进行设计的,而是任意对象,它所负责的工作就是不断地向对象的某个属性赋值,然后根据属性值的改变再来决定如何展示出来。比如下面这段代码的第一句含义就是使用ObjectAnimator不断地修改ImageView的rotation属性,从0°到360°,然后ImageView对象则需要根据rotation值不断刷新显示,然后展现出旋转动画效果。1
2
3
4
5
6
7
8
9// 旋转动画,从0°到360°
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mImageView, "rotation", 0f, 360f);
// 设置动画重复次数
objectAnimator.setRepeatCount(10);
// 设置动画重复模式为反转
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
// 设置动画时长
objectAnimator.setDuration(1000);
objectAnimator.start();同样,以下是淡入淡出动画:
1
2
3
4
5// 淡入淡出动画,让ImageView的alpha值从1到0再到1
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mImageView, "alpha", 1f, 0f, 1f);
// 设置动画的时长
objectAnimator.setDuration(3000);
objectAnimator.start();水平缩放动画:
1
2
3
4// 将ImageView水平放大3倍,然后再缩小至原有大小
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mImageView, "scaleX", 1f, 3f, 1f);
objectAnimator.setDuration(1000);
objectAnimator.start();平移动画:
1
2
3
4
5// 获取ImageView当前的水平位置,然后将其水平移动到-500f,然后再移动到初始位置
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mImageView, "translationX", mImageView.getTranslationX(), -500f, mImageView.getTranslationX());
// 设置动画时长
objectAnimator.setDuration(5000);
objectAnimator.start();需要注意的是,缩放动画跟反转动画都是有中心点跟中心轴的,默认是控件的中心为中心点,如果需要自己设置动画的中心点则需要调用控件的
setPivotX()
跟setPivotY()
方法设置中心点,然后显示调用invalidate()
方法。1
2
3
4
5
6
7
8
9// 设置中心轴
mImageView.setPivotX(0);
mImageView.setPivotY(0);
// 手动调用invalidate方法
mImageView.invalidate();
// 将TextView水平放大3倍,然后再缩小至原有大小
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mImageView, "scaleX", 1f, 3f, 1f);
objectAnimator.setDuration(1000);
objectAnimator.start();
组合动画
如果我们需要将几个动画组合执行,则需要借助AnimatorSet类,该类提供了几个方法用于控制动画的执行顺序,我们向play()
方法中传入一个动画对象,然后通过一系列方法控制其他动画与该动画的播放顺序。
after(Animator anim)
:是将play(Animator anim)
中的动画放在after(Animator anim)
方法中的动画之后执行。after(long delay)
:该方法是将play(Animator anim)
方法的动画延后执行,延长的时间根据传入的参数而定。before(Animator anim)
:是将play(Animator anim)
中的动画放在before(Animator anim)
方法中的动画之前执行。与after(Animator anim)作用相反。with(Animator anim)
:该方法是将play(Animator anim)
的动画跟with(Animator anim)
中传入的动画同时执行。playTogether(Animator... items)
:该方法可以将传入的多个Animator对象同时执行。
1 | /** |
以上代码是先执行alphaAnimation跟scaleAnimation的组合动画,然后再执行translationAnimation。
动画的监听事件
如果要对动画进行监听,则需要使用Animator类的
addListener()
方法并传入一个实现了AnimatorListener接口的参数,由于ValueAnimator跟AnimatorSet都是继承自Animator的,而ObjectAnimator又是继承自ValueAnimator的,所以它们都可以使用该方法添加动画的监听。实现AnimatorListener接口需要重写四个方法,我们可以根据需求在对应的方法里实现相应的逻辑处理,如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22// 动画的监听
objectAnimator.addListener(new Animator.AnimatorListener() {
public void onAnimationStart(Animator animator) {
// 动画开始时调用
}
public void onAnimationEnd(Animator animator) {
// 动画结束时调用
}
public void onAnimationCancel(Animator animator) {
// 动画被取消时调用
}
public void onAnimationRepeat(Animator animator) {
// 动画重复执行时调用
}
});如果不想一次覆写这么多的方法,那么可以在
addListener()
方法中传入AnimatorListenerAdapter对象,由于AnimatorListenerAdapter对象已经实现了AnimatorListener接口,所以我们只需要按需求重写其中任意一个或多个方法即可,即使一个都不重写也不会报错。如下所示:1
2
3
4
5
6objectAnimator.addListener(new AnimatorListenerAdapter() {
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
使用xml编写动画:
将一些常用的动画使用xml编写,可以实现复用。首先需要在res目录下新建animator文件夹,该文件夹专门用于存放动画xml文件。xml文件三种标签的对应关系分别为:
对应代码中的ValueAnimator; 对应代码中的ObjectAnimator; 对应代码中的AnimatorSet。
比如,实现从0到1的变化:
1 | <?xml version="1.0" encoding="utf-8"?> |
如果需要对一个控件的属性进行操作则可以:
1 | <?xml version="1.0" encoding="utf-8"?> |
如果需要实现组合动画则:
1 | <?xml version="1.0" encoding="utf-8"?> |
组合动画中set节点下的android:ordering
属性就是控制动画执行顺序的:
together
表示该set标签下的动画是同时执行的sequentially
表示该set标签下的动画是顺序按编写顺序执行的
动画编写完成后需要在代码中加载使用,使用AnimatorInflater的loadAnimator()
方法,将动画文件加载后赋给animator对象,然后再使用Animator对象的setTarget()
方法传入需要执行动画的目标对象,然后调用start()方法执行动画。
1 | Animator animator = AnimatorInflater.loadAnimator(this, R.animator.animator_copbined); |
以上便是属性动画的基本用法。