一. 属性动画的组合

1. AnimatorSet类

AnimatorSet类 用来实现 组合动画 的功能,常用方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//播放当前动画
public Builder play(Animator anim);

//将现有动画和传入的动画同时执行
public Builder with(Animator anim);

//将现有动画插入到传入的动画之前执行
public Builder before(Animator anim);

//将现有动画插入到传入的动画之后执行
public Builder after(Animator anim);

//将现有动画延迟 delay ms 执行
public Builder after(long delay);

//同时执行
public void playTogether(Animator... items);
public void playTogether(Collection<Animator> items);

//按顺序执行
public void playSequentially(Animator... items);
public void playSequentially(List<Animator> items);

2. set标签

set标签中只有一个属性 android:ordering:表示动画开始顺序,取值有两个,together表示同时开始动画,sequentially表示逐个开始动画。

3. 实际例子

① Java 方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Animation_java_Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_java_);

//找到需要动画的控件
Button button = findViewById(R.id.btn);

//动画组合类
final AnimatorSet animatorSet = new AnimatorSet();

//平移动画
ObjectAnimator translationX = ObjectAnimator.ofFloat(button, "TranslationX", 0.0f, 200.0f);
translationX.setDuration(2000);

//旋转动画
ObjectAnimator rotation = ObjectAnimator.ofFloat(button, "Rotation", 0.0f, 360.0f);
rotation.setDuration(3000);

//透明动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(button, "Alpha", 1.0f, 0.5f);
alpha.setDuration(2000);

//组合动画
animatorSet.play(translationX).with(rotation).before(alpha);

//触发动画
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatorSet.start();
}
});
}
}

② Xml方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">

<set android:ordering="together">
<objectAnimator
android:valueFrom="0"
android:valueTo="200"
android:valueType="floatType"
android:propertyName="TranslationX"
android:duration="2000"
/>

<objectAnimator
android:valueFrom="0"
android:valueTo="360"
android:valueType="floatType"
android:propertyName="Rotation"
android:duration="3000"
/>
</set>

<objectAnimator
android:valueFrom="1"
android:valueTo="0.5"
android:valueType="floatType"
android:propertyName="Alpha"
android:duration="2000"
/>

</set>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Animation_xml_Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_xml_);

//找到需要动画的控件
Button button = findViewById(R.id.btn);

//加载动画文件
final AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.set_animation);

//设置动画对象
animatorSet.setTarget(button);

//触发动画
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatorSet.start();
}
});
}
}

二. 快捷使用动画

ViewPropertyAnimator类是属性动画的一种简写、快捷使用方式.

1
View.animate().xxx().xxx();
  • ViewPropertyAnimator的功能建立在animate()上。

  • 调用animate()方法返回值是一个ViewPropertyAnimator对象,之后的调用的所有方法都是通过该实例完成。

  • 调用该实例的各种方法来实现动画效果。

  • ViewPropertyAnimator所有接口方法都使用连缀语法来设计,每个方法的返回值都是它自身的实例。

  • 因此调用完一个方法后可直接连缀调用另一方法,即可通过一行代码就完成所有动画效果。

实例

1
2
3
4
5
6
7
8
9
10
11
button = (Button) findViewById(R.id.Button);
//创建动画作用对象:此处以Button为例

button.animate().alpha(0f);
//单个动画设置:将按钮变成透明状态

button.animate().alpha(0f).setDuration(5000).setInterpolator(new BounceInterpolator());
//单个动画效果设置 & 参数设置

button.animate().alpha(0f).x(500).y(500);
//组合动画:将按钮变成透明状态再移动到(500,500)处
  • 动画自动启动,无需调用start()方法.因为新的接口中使用了隐式启动动画的功能,只要我们将动画定义完成后,动画就会自动启动>

  • 如果不想使用这一默认机制,也可以显式地调用start()方法来启动动画。

三. 监听动画

1. 简介

Animation类通过监听动画开始 / 结束 / 重复 / 取消时刻来进行一系列操作,如跳转页面等等

2. 具体使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       Animation.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始时执行
}

@Override
public void onAnimationRepeat(Animation animation) {
//动画重复时执行
}

@Override
public void onAnimationCancel()(Animation animation) {
//动画取消时执行
}

@Override
public void onAnimationEnd(Animation animation) {
//动画结束时执行
}
});

// 特别注意:每次监听必须4个方法都重写。

3. 动画适配器

1
2
3
4
5
6
7
8
9
anim.addListener(new AnimatorListenerAdapter() {  
// 向addListener()方法中传入适配器对象AnimatorListenerAdapter()
// 由于AnimatorListenerAdapter中已经实现好每个接口
// 所以这里不实现全部方法也不会报错
@Override
public void onAnimationStart(Animator animation) {
// 如想只想监听动画开始时刻,就只需要单独重写该方法就可以
}
});

参考文章

Android 动画:这些属性动画的使用小技巧你了解吗

安卓动画学习(六)–xml实现属性动画