一、概念
動(dòng)畫集合,子類:AnimationSet,標(biāo)簽:<set>
它可以包含若干個(gè)動(dòng)畫,并且它的內(nèi)部也可以嵌套其它動(dòng)畫集合。
二、實(shí)現(xiàn)
1. XML實(shí)現(xiàn)
//res/anim/set_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="300"
android:toYDelta="300"
/>
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotX="0%"
android:pivotY="0%"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1" />
</set>
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500px"
android:layout_height="500px"
android:layout_marginLeft="100px"
android:background="@color/colorAccent">
<TextView
android:id="@+id/animate_tv"
android:layout_width="wrap_content"
android:layout_height="100px"
android:gravity="center"
android:textColor="@color/colorAccent"
android:text="Hello Android!"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
//代碼,MainActivity
private void setAnimationXML() {
Animation setAnimation = AnimationUtils.loadAnimation(this, R.anim.set_animation);
mAnimate_tv.setAnimation(setAnimation);
mAnimate_tv.startAnimation(setAnimation);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setAnimationXML();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}
2. 代碼實(shí)現(xiàn)
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500px"
android:layout_height="500px"
android:layout_marginLeft="100px"
android:background="@color/colorAccent">
<TextView
android:id="@+id/animate_tv"
android:layout_width="wrap_content"
android:layout_height="100px"
android:gravity="center"
android:textColor="@color/colorAccent"
android:text="Hello Android!"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
//代碼,MainActivity
private void setAnimationCode() {
AnimationSet animationSet = new AnimationSet(true); //參數(shù)為true表示子動(dòng)畫共用一個(gè)插值器
animationSet.addAnimation(new TranslateAnimation(0, 300, 0, 300));
animationSet.addAnimation(new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
animationSet.addAnimation(new AlphaAnimation(0.1f, 1f));
animationSet.setDuration(3000);
animationSet.setFillAfter(true);
mAnimate_tv.setAnimation(animationSet);
mAnimate_tv.startAnimation(animationSet);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setAnimationCode();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}
三、屬性
android:interpolator:
表示動(dòng)畫集合所采用的插值器,插值器影響動(dòng)畫的速度。比如非勻速動(dòng)畫就需要通過插值器來控制動(dòng)畫的播放過程。這個(gè)屬性可以不指定,默認(rèn)為@android:anim/accelerate_decelerate_interpolator,即加速減速插值器。
android:shareInterpolator:
表示集合中的動(dòng)畫是否和集合共享同一個(gè)插值器。如果集合不指定插值器,那么子動(dòng)畫就需要單獨(dú)指定所需的插值器或者使用默認(rèn)插值器。