一. 属性动画的组合
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);
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.animate().alpha(0f);
button.animate().alpha(0f).setDuration(5000).setInterpolator(new BounceInterpolator());
button.animate().alpha(0f).x(500).y(500);
|
三. 监听动画
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) { } });
|
3. 动画适配器
1 2 3 4 5 6 7 8 9
| anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { } });
|
参考文章
Android 动画:这些属性动画的使用小技巧你了解吗
安卓动画学习(六)–xml实现属性动画