一、動畫類型
Android的animation由四種類型組成:alpha、scale、translate、rotate
XML配置文件中
alpha | 漸變透明度動畫效果 |
scale | 漸變尺寸伸縮動畫效果 |
translate | 畫面轉換位置移動動畫效果 |
rotate | 畫面轉移旋轉動畫效果 |
?
?
?
Java Code代碼中?
AlphaAnimation | 漸變透明度動畫效果 |
ScaleAnimation | 漸變尺寸伸縮動畫效果 |
TranslateAnimation | 畫面轉換位置移動動畫效果 |
RotateAnimation | 畫面轉移旋轉動畫效果 |
?
?
二、Android動畫模式
Animation主要有兩種動畫模式:tweened?和?frame
- 一種是tweened animation(漸變動畫)?
XML中 | JavaCode |
alpha | AlphaAnimation |
scale | ScaleAnimation |
?
?
?
?
- 一種是frame by frame(畫面轉換動畫)?
XML中 | JavaCode |
translate | TranslateAnimation |
rotate | RotateAnimation |
?
?
?
三、XML文件中定義動畫
?
① 打開Eclipse,新建Android工程
② 在res目錄中新建anim文件夾
③ 在anim目錄中新建一個myanim.xml(注意文件名小寫)
④ 加入XML的動畫代碼
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><alpha/><scale/><translate/><rotate/> </set>
四、Android?XML動畫解析
1、Alpha
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="3000" /> <!-- 透明度控制動畫效果 alpha浮點型值:fromAlpha 屬性為動畫起始時透明度toAlpha 屬性為動畫結束時透明度說明: 0.0表示完全透明1.0表示完全不透明以上值取0.0-1.0之間的float數據類型的數字長整型值:duration 屬性為動畫持續時間說明: 時間以毫秒為單位 --> </set>
2、Scale
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromXScale="0.0"android:toXScale="1.4"android:fromYScale="0.0"android:toYScale="1.4"android:pivotX="50%"android:pivotY="50%"android:fillAfter="false"android:duration="700" /> </set> <!-- 尺寸伸縮動畫效果 scale屬性:interpolator 指定一個動畫的插入器在我試驗過程中,使用android.res.anim中的資源時候發現有三種動畫插入器:accelerate_decelerate_interpolator 加速-減速 動畫插入器accelerate_interpolator 加速-動畫插入器decelerate_interpolator 減速- 動畫插入器其他的屬于特定的動畫效果浮點型值:fromXScale 屬性為動畫起始時 X坐標上的伸縮尺寸 toXScale 屬性為動畫結束時 X坐標上的伸縮尺寸 fromYScale 屬性為動畫起始時Y坐標上的伸縮尺寸 toYScale 屬性為動畫結束時Y坐標上的伸縮尺寸 說明:以上四種屬性值 0.0表示收縮到沒有 1.0表示正常無伸縮 值小于1.0表示收縮 值大于1.0表示放大pivotX 屬性為動畫相對于物件的X坐標的開始位置pivotY 屬性為動畫相對于物件的Y坐標的開始位置說明:以上兩個屬性值 從0%-100%中取值50%為物件的X或Y方向坐標上的中點位置長整型值:duration 屬性為動畫持續時間說明: 時間以毫秒為單位布爾型值:fillAfter 屬性 當設置為true ,該動畫轉化在動畫結束后被應用 -->
3、Translate
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="30" android:toXDelta="-80" android:fromYDelta="30" android:toYDelta="300" android:duration="2000" /> <!-- translate 位置轉移動畫效果整型值:fromXDelta 屬性為動畫起始時 X坐標上的位置 toXDelta 屬性為動畫結束時 X坐標上的位置fromYDelta 屬性為動畫起始時 Y坐標上的位置toYDelta 屬性為動畫結束時 Y坐標上的位置注意:沒有指定fromXType toXType fromYType toYType 時候,默認是以自己為相對參照物 長整型值:duration 屬性為動畫持續時間說明: 時間以毫秒為單位 --> </set>
4、Rotate
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="3000" /> <!-- rotate 旋轉動畫效果屬性:interpolator 指定一個動畫的插入器在我試驗過程中,使用android.res.anim中的資源時候發現有三種動畫插入器:accelerate_decelerate_interpolator 加速-減速 動畫插入器accelerate_interpolator 加速-動畫插入器decelerate_interpolator 減速- 動畫插入器其他的屬于特定的動畫效果浮點數型值:fromDegrees 屬性為動畫起始時物件的角度 toDegrees 屬性為動畫結束時物件旋轉的角度 可以大于360度 說明:當角度為負數——表示逆時針旋轉當角度為正數——表示順時針旋轉 (負數from——to正數:順時針旋轉) (負數from——to負數:逆時針旋轉) (正數from——to正數:順時針旋轉) (正數from——to負數:逆時針旋轉) pivotX 屬性為動畫相對于物件的X坐標的開始位置pivotY 屬性為動畫相對于物件的Y坐標的開始位置說明: 以上兩個屬性值 從0%-100%中取值50%為物件的X或Y方向坐標上的中點位置長整型值:duration 屬性為動畫持續時間說明: 時間以毫秒為單位 --> </set>
XML中使用動畫效果
public static Animation loadAnimation (Context context, int id) //第一個參數Context為程序的上下文 //第二個參數id為動畫XML文件的引用 //例子: myAnimation= AnimationUtils.loadAnimation(this, R.anim.my_action); //使用AnimationUtils類的靜態方法loadAnimation()來加載XML中的動畫XML文件
五、Java代碼中定義動畫
//在代碼中定義 動畫實例對象 private Animation myAnimation_Alpha; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation myAnimation_Rotate;//根據各自的構造方法來初始化一個實例對象 myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);myAnimation_Translate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
六、Android 代碼動畫解析
1. AlphaAnimation
AlphaAnimation類對象定義
?
private AlphaAnimation myAnimation_Alpha;
AlphaAnimation類對象構造
AlphaAnimation(float fromAlpha, float toAlpha) //第一個參數fromAlpha為 動畫開始時候透明度 //第二個參數toAlpha為 動畫結束時候透明度 myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f); //說明: // 0.0表示完全透明 // 1.0表示完全不透明
設置動畫持續時間
myAnimation_Alpha.setDuration(5000); //設置時間持續時間為 5000毫秒
2. ScaleAnimation
ScaleAnimation類對象定義
private ScaleAnimation myAnimation_Scale;
ScaleAnimation類對象構造
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //第一個參數fromX為動畫起始時 X坐標上的伸縮尺寸 //第二個參數toX為動畫結束時 X坐標上的伸縮尺寸 //第三個參數fromY為動畫起始時Y坐標上的伸縮尺寸 //第四個參數toY為動畫結束時Y坐標上的伸縮尺寸 /*說明:以上四種屬性值 0.0表示收縮到沒有 1.0表示正常無伸縮 值小于1.0表示收縮 值大于1.0表示放大 */ //第五個參數pivotXType為動畫在X軸相對于物件位置類型 //第六個參數pivotXValue為動畫相對于物件的X坐標的開始位置 //第七個參數pivotXType為動畫在Y軸相對于物件位置類型 //第八個參數pivotYValue為動畫相對于物件的Y坐標的開始位置 myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
設置動畫持續時間
myAnimation_Scale.setDuration(700); //設置時間持續時間為 700毫秒
3. TranslateAnimation
ranslateAnimation類對象定義
private TranslateAnimation myAnimation_Translate;
TranslateAnimation類對象構造
TranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta, float toYDelta) //第一個參數fromXDelta為動畫起始時 X坐標上的移動位置 //第二個參數toXDelta為動畫結束時 X坐標上的移動位置 //第三個參數fromYDelta為動畫起始時Y坐標上的移動位置 //第四個參數toYDelta為動畫結束時Y坐標上的移動位置
設置動畫持續時間
myAnimation_Translate = new TranslateAnimation(10f, 100f, 10f, 100f); myAnimation_Translate.setDuration(2000); //設置時間持續時間為 2000毫秒
4. RotateAnimation
RotateAnimation類對象定義
private RotateAnimation myAnimation_Rotate;
RotateAnimation類對象構造
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //第一個參數fromDegrees為動畫起始時的旋轉角度 //第二個參數toDegrees為動畫旋轉到的角度 //第三個參數pivotXType為動畫在X軸相對于物件位置類型 //第四個參數pivotXValue為動畫相對于物件的X坐標的開始位置 //第五個參數pivotXType為動畫在Y軸相對于物件位置類型 //第六個參數pivotYValue為動畫相對于物件的Y坐標的開始位置 myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
設置動畫持續時間
myAnimation_Rotate.setDuration(3000); //設置時間持續時間為 3000毫秒
如何Java代碼中使用動畫效果
使用從View父類繼承過來的方法startAnimation()來為View或是子類View等等添加一個動畫效果
public void startAnimation (Animation animation) view.startAnimation(myAnimation_Alpha); view.startAnimation(myAnimation_Scale); view.startAnimation(myAnimation_Translate); view.startAnimation(myAnimation_Rotate);
?