Android属性动画使用技巧

组合动画

实现 组合动画 的功能: AnimatorSet

1
2
3
4
5
AnimatorSet.play(Animator anim)   :播放当前动画
AnimatorSet.after(long delay) :将现有动画延迟x毫秒后执行
AnimatorSet.with(Animator anim) :将现有动画和传入的动画同时执行
AnimatorSet.after(Animator anim) :将现有动画插入到传入的动画之后执行
AnimatorSet.before(Animator anim) : 将现有动画插入到传入的动画之前执行
s

###### 实例练习

实现效果:主要动画是平移,平移过程中伴随旋转动画,平移完后进行透明度变化

> 实现方式有 XML设置 / Java代码设置



方式1:Java代码设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 步骤1:设置需要组合的动画效果
ObjectAnimator translation = ObjectAnimator.ofFloat(mButton, "translationX", curTranslationX, 300,curTranslationX);
// 平移动画
ObjectAnimator rotate = ObjectAnimator.ofFloat(mButton, "rotation", 0f, 360f);
// 旋转动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(mButton, "alpha", 1f, 0f, 1f);
// 透明度动画

// 步骤2:创建组合动画的对象
AnimatorSet animSet = new AnimatorSet();

// 步骤3:根据需求组合动画
animSet.play(translation).with(rotate).before(alpha);
animSet.setDuration(5000);

// 步骤4:启动动画
animSet.start();

效果:

方式2: XML 设置

set_animation.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially" >
// 表示Set集合内的动画按顺序进行
// ordering的属性值:sequentially & together
// sequentially:表示set中的动画,按照先后顺序逐步进行(a 完成之后进行 b )
// together:表示set中的动画,在同一时间同时进行,为默认值

<set android:ordering="together" >
// 下面的动画同时进行
<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="300"
android:valueType="floatType" >
</objectAnimator>

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

<set android:ordering="sequentially" >
// 下面的动画按序进行
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType" >
</objectAnimator>
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" >
</objectAnimator>
</set>

</set>

在java代码中启用动画

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

AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_animation);
// 创建组合动画对象 & 加载XML动画
animator.setTarget(mButton);
// 设置动画作用对象
animator.start();
// 启动动画

基本上和前面学的差不多。

快捷使用
简介
  • 从属性动画的原理可知,属性动画的本质是:对值进行操作
  • Java是面向对象的,所以 Google 团队添加面向对象操作的属性动画使用 - ViewPropertyAnimator
  • 可认为是属性动画的一种简写、快捷使用方式
使用
1
2
3
4
5
6
View.animate().xxx().xxx();
// ViewPropertyAnimator的功能建立在animate()上
// 调用animate()方法返回值是一个ViewPropertyAnimator对象,之后的调用的所有方法都是通过该实例完成
// 调用该实例的各种方法来实现动画效果
// ViewPropertyAnimator所有接口方法都使用连缀语法来设计,每个方法的返回值都是它自身的实例
// 因此调用完一个方法后可直接连缀调用另一方法,即可通过一行代码就完成所有动画效果

实例:

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

mButton.animate().alpha(0f);
// 单个动画设置:将按钮变成透明状态
mButton.animate().alpha(0f).setDuration(5000).setInterpolator(new BounceInterpolator());
// 单个动画效果设置 & 参数设置
mButton.animate().alpha(0f).x(500).y(500);
// 组合动画:将按钮变成透明状态再移动到(500,500)处

// 特别注意:
// 动画自动启动,无需调用start()方法.因为新的接口中使用了隐式启动动画的功能,只要我们将动画定义完成后,动画就会自动启动
// 该机制对于组合动画也同样有效,只要不断地连缀新的方法,那么动画就不会立刻执行,等到所有在ViewPropertyAnimator上设置的方法都执行完毕后,动画就会自动启动
// 如果不想使用这一默认机制,也可以显式地调用start()方法来启动动画

有很多可以连缀的语法,用到时候再去查。

监听动画

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

使用
  • 通过在Java代码里addListener()设置
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个方法都重写。
注意
  • Animator类、AnimatorSet类、ValueAnimatorObjectAnimator类存在以下继承关系

  • 所以AnimatorSet类、ValueAnimatorObjectAnimator都可以使用addListener()监听器进行动画监听
动画适配器

有些时候我们并不需要监听动画的所有时刻,但addListener(new AnimatorListener())监听器是必须重写4个时刻方法,这使得接口方法重写太累赘,因此采用动画适配器(AnimatorListenerAdapter),解决实现接口繁琐 的问题

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

https://github.com/Commandercc/DemoEX/blob/master/AnimationDmeo3.zip

0%