Animations的使用(3)
1 AnimationSet的使用方法
什么是AnimationSet
1 AnimationSet是Animation的子類
2 一個AnimationSet包含了一系列的Animation
3 針對AnimationSet設置一些Animation的常見屬性(如StartOffset,duration等),可以被包含在AnimationSet當中的Animation繼承
使用步驟:(類似1中的例子 只不過含有2個動畫效果)
- AnimationSet animationSet = new AnimationSet(ture);
- AlpahaAnimation alpha = new AlphaAnimation(...);
- RotateAnimation rotate = new RotateAnimation(...);
- animationSet.addAnimation(alpha);
- animationSet.addAnimaion(rotate);
- animationSet.setDuration(2000);
- animationSet.setStartOffset(500);
- imageView.startAnimation(animationSet);
AnimationSet animationSet = new AnimationSet(ture); AlpahaAnimation alpha = new AlphaAnimation(...); RotateAnimation rotate = new RotateAnimation(...); animationSet.addAnimation(alpha); animationSet.addAnimaion(rotate); animationSet.setDuration(2000); animationSet.setStartOffset(500); imageView.startAnimation(animationSet);
2 Interpolator的使用方法
Interpolator定義了動畫變化速率,在Animations框架中定義了以下幾種Interpolator
AccelerateDecelerateInterpolator:在動畫開始和結束的地方速率變化較慢,中間的時候加速
AccelerateInterpolator:在動畫開始的地方速率改變較慢,然后加速
CycleInterpolator:動畫循環播放特定次數,速率改變沿正弦曲線
DecelerateInterpolator:在動畫開始的地方速率改變較慢,然后減速
LinearInterpolator:以均勻的速率改變
設置的地方就在set標簽中的 android:interpolator="@android:anim/accelerate_interpolator"
而之后還有一個android:shareInterpolator="true" 從名字就可以看到這是為set中所有的動畫設置Interpolator
如果要單獨設置 則將shareInterpolator設為false 然后為每個動畫中單獨定義Interpolator
以上是在xml中設置,如果要在代碼中設置
animationSet.setInterpolator(new AccelerateInterpolator());(也可以單獨設置)
注意在AnimationSet的構造方法中有一個boolean參數,這個參數就是shareInterpolator的設定
3 Frame-By-Frame Animations的使用方法
1 在res/drawable中創建一個xml文件,定義Animation的動畫播放序列 anim_nv.xml
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/nv1"
- android:duration="500" />
- <item android:drawable="@drawable/nv2"
- android:duration="500" />
- <item android:drawable="@drawable/nv3"
- android:duration="500" />
- <item android:drawable="@drawable/nv4"
- android:duration="500" />
- </animation-list>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/nv1" android:duration="500" /> <item android:drawable="@drawable/nv2" android:duration="500" /> <item android:drawable="@drawable/nv3" android:duration="500" /> <item android:drawable="@drawable/nv4" android:duration="500" /> </animation-list>
2 為ImageView設置背景資源
- imageView.setBackgroundResource(R.drawable.anim_nv);
imageView.setBackgroundResource(R.drawable.anim_nv);
3 通過ImageView得到AnimationDrawable
- AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
3 執行動畫
- animationDrawable.start();
animationDrawable.start();