一、概念
动画集合,子类:AnimationSet,标签:<set>
它可以包含若干个动画,并且它的内部也可以嵌套其它动画集合。
二、实现
1. XML实现
//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. 代码实现
//布局
<?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); //参数为true表示子动画共用一个插值器
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:
表示动画集合所采用的插值器,插值器影响动画的速度。比如非匀速动画就需要通过插值器来控制动画的播放过程。这个属性可以不指定,默认为@android:anim/accelerate_decelerate_interpolator,即加速减速插值器。
android:shareInterpolator:
表示集合中的动画是否和集合共享同一个插值器。如果集合不指定插值器,那么子动画就需要单独指定所需的插值器或者使用默认插值器。