android动画之Interpolator和AnimationSet

如果在android中开发复合动画需要使用类AnimationSet 类 利用它的add 方法就可一加入动画 或者使用布局文件 set 加入多个动画就行  不过他们是在同一时间内开始动画的

 1 xml代码
 2 <?xml version="1.0"encoding="utf-8"?>
 3 <setxmlns:android="http: android=""res=""apk=""schemas.android.com="">  
 4  <translate 
 5     android:toXDelta="100%p" 
 6     android:fromXDelta="0" 
 7     android:duration="1000">    
 8     </alpha>
 9 </translate>
10 </setxmlns:android="http:>
 1 //    通过AnimationSet应用多个动画
 2 //    AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。
 3 //    以下例子同时应用5个动画:
 4 //          播放anim1;
 5 //          同时播放anim2,anim3,anim4;
 6 //          播放anim5。      
 7     AnimatorSet bouncer = new AnimatorSet();
 8       bouncer.play(anim1).before(anim2);
 9       bouncer.play(anim2).with(anim3);
10       bouncer.play(anim2).with(anim4)
11       bouncer.play(anim5).after(amin2);
12       animatorSet.start();

AnimationSet可以加入Animation,加入之后设置AnimationSet对加入的所有Animation都有效。

 1 AnimationSet anim=new AnimationSet(true); 
 2              AlphaAnimation a=new AlphaAnimation(1,0); 
 3              RotateAnimation ra=new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); 
 4              anim.addAnimation(a); 
 5              anim.addAnimation(ra); 
 6              anim.setDuration(3000); 
 7              anim.setStartOffset(1000); 
 8              iv.startAnimation(anim); 
 9  
10 AnimationSet anim=new AnimationSet(true);
11     AlphaAnimation a=new AlphaAnimation(1,0);
12     RotateAnimation ra=new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
13     anim.addAnimation(a);
14     anim.addAnimation(ra);
15     anim.setDuration(3000);
16     anim.setStartOffset(1000);
17     iv.startAnimation(anim);

可以再xml文件中定义多个Animation,这样多个Animation可以一起运行

 1 <?xml version="1.0" encoding="utf-8"?> 
 2  <set xmlns:android="http://schemas.android.com/apk/res/android" 
 3      android:interpolator="@android:anim/accelerate_interpolator" 
 4      android:shareInterpolator="true" 
 5      > 
 6      <alpha 
 7          android:fromAlpha="1.0" 
 8          android:toAlpha="0.0" 
 9          android:startOffset="500" 
10          android:duration="3000" 
11              /> 
12      <rotate 
13          android:fromDegrees="0" 
14          android:toDegrees="400" 
15          android:pivotX="50%" 
16          android:pivotY="50%" 
17          android:duration="3000" 
18      /> 
19   
20  </set> 
 1 <?xml version="1.0" encoding="utf-8"?>
 2  <set xmlns:android="http://schemas.android.com/apk/res/android"
 3      android:interpolator="@android:anim/accelerate_interpolator"
 4      android:shareInterpolator="true"
 5      >
 6      <alpha
 7          android:fromAlpha="1.0"
 8          android:toAlpha="0.0"
 9          android:startOffset="500"
10          android:duration="3000"
11              />
12      <rotate
13          android:fromDegrees="0"
14          android:toDegrees="400"
15          android:pivotX="50%"
16          android:pivotY="50%"
17          android:duration="3000"
18      />
19  
20 </set>

Interpolator可以定义动画播放的速度

在xml文件中定义Interpolator

android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"

  

这样所有的Animation共用一个Interpolator。

在代码中用代码设置如下

 anim.setInterpolator(new AccelerateInterpolator()); 

在new一个AnimationSet中传入true则所有的Animation共用Interpolator

原文地址:https://www.cnblogs.com/laxlerbo/p/3835856.html