Animations一般分為兩大類,一類是漸變的(Tweened):如淡入淡出,旋轉,移動,縮放;另一類是Frame-by-Frame,就如電影一般由多張圖片按照一定的時間間隔顯示。
使用Tweened Animations的第一種使用步驟:
1. 創建一個AnimationSet對象,AnimationSet animationSet = new AnimationSet (boolean b); //b為true表示共享Interpolator
2. 根據需要創建相應的Animation對象(AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation)
3. 根據軟件動畫的需求,為Animation對象設置相應的數據
4. 將Animation對象添加到AnimationSet對象當中,使用addAnimation方法
5. 使用控件對象開始執行AnimationSet
使用Tweened Animations的第二種使用步驟:
1. 在res文件夾下面新建一個名為anim的文件夾
2. 創建xml文件,並首先加入set標簽,改標簽如下:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> //interpolator定義動畫變化的速率
</set>
3. 在該標簽當中加入rotate,alpha,scale或者translate標簽,例子如下:
<alpha
android:fromAlpha = "0.1"
android:toAlpha = "1.0"
android:duration = "3000"/>
<rotate
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
---注意---android:pivotX的值共有三種設置方法:
1. android:pivotX="50"這種方法使用絕對位置定位
2. android:pivotX="50%"這種方法相對於控件本身定位
3. android:pivotX="50%p"這種方法相對於控件的父控件定位
4. 在代碼當中使用AnimationUtils當中裝載xml文件,並生成Animation對象,AnimationUtils.loadAnimation
Frame-By-Frame:使用方法
1. 在res/drawable當中創建一個xml文件,用於定義Animations的動畫序列,以下是例子:
<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. setBackgroundResource("在res/drawable當中創建一個xml文件的Id");
3. AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
4. animationDrawable.start();