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