一、概念
AnimatorSet,标签:<set>
属性动画集合。
二、实现
1. XML实现
//res/animator/set_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:propertyName="alpha"
android:duration="3000"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType"
/>
<objectAnimator
android:propertyName="translationX"
android:duration="3000"
android:valueFrom="0"
android:valueTo="200"
android:valueType="floatType"
/>
<objectAnimator
android:propertyName="translationY"
android:duration="3000"
android:valueFrom="0"
android:valueTo="200"
android:valueType="floatType"
/>
</set>
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="100px"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="Hello Android!"
android:background="@color/colorPrimary"/>
</RelativeLayout>
//代码,MainActivity
private void setAnimatorXML() {
mAnimatorSet = (AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.set_animator);
mAnimatorSet.setTarget(mMain_tv);
mAnimatorSet.start();
}
private void stopAnimator() {
mAnimatorSet.cancel();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setAnimatorXML();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimator();
}
2. 代码实现
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="100px"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="Hello Android!"
android:background="@color/colorPrimary"/>
</RelativeLayout>
//代码,MainActivity
private void setAnimatorCode() {
ObjectAnimator animator1 = ObjectAnimator.ofFloat(mMain_tv, "alpha", 0, 1);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mMain_tv, "translationX", 0, 200);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mMain_tv, "translationY", 0, 200);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animator1, animator2, animator3);
animatorSet.setDuration(3000);
animatorSet.start();
}
private void stopAnimator() {
mAnimatorSet.cancel();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setAnimatorCode();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimator();
}
三、属性
android:ordering:
表示动画集合中的子动画播放顺序。有"together"和"sequentially"两个选项,默认值为"together"。
其中"together"表示动画集合中的子动画同时播放,"sequentially"表示动画集合中的子动画按照前后顺序依次播放。