前言
- 動(dòng)畫的使用 是 Android 開發(fā)中常用的知識(shí)
- 可是動(dòng)畫的種類繁多、使用復(fù)雜羽峰,每當(dāng)需要 采用自定義動(dòng)畫 實(shí)現(xiàn) 復(fù)雜的動(dòng)畫效果時(shí)番刊,很多開發(fā)者就顯得束手無策
-
本文將詳細(xì)介紹 Android 動(dòng)畫中 補(bǔ)間動(dòng)畫的原理 & 使用
動(dòng)畫類型
目錄
1. 作用對(duì)象
視圖控件(View
)
- 如Android的TextView、Button等等
- 不可作用于View組件的屬性形用,如:顏色奥裸、背景险掀、長(zhǎng)度等等
2. 原理
通過確定開始的視圖樣式 & 結(jié)束的視圖樣式、中間動(dòng)畫變化過程由系統(tǒng)補(bǔ)全來確定一個(gè)動(dòng)畫
- 結(jié)束的視圖樣式:平移湾宙、縮放樟氢、旋轉(zhuǎn) & 透明度樣式
- 即補(bǔ)間動(dòng)畫的動(dòng)畫效果就是:平移冈绊、縮放、旋轉(zhuǎn) & 透明度動(dòng)畫
3. 分類
根據(jù)不同的動(dòng)畫效果埠啃,補(bǔ)間動(dòng)畫分為4種動(dòng)畫:
- 平移動(dòng)畫(
Translate
) - 縮放動(dòng)畫(
scale
) - 旋轉(zhuǎn)動(dòng)畫(
rotate
) - 透明度動(dòng)畫(
alpha
)
同時(shí)死宣,不同類型的動(dòng)畫對(duì)應(yīng)于不同的子類,具體如下圖:
下面在介紹補(bǔ)間動(dòng)畫使用時(shí)碴开,會(huì)詳細(xì)介紹上述四種動(dòng)畫
4. 具體使用
- 補(bǔ)間動(dòng)畫的使用方式分為兩種:在XML 代碼 / Java 代碼里設(shè)置
前者優(yōu)點(diǎn):動(dòng)畫描述的可讀性更好
后者優(yōu)點(diǎn):動(dòng)畫效果可動(dòng)態(tài)創(chuàng)建
下面我將詳細(xì)平移毅该、縮放、旋轉(zhuǎn) & 透明度動(dòng)畫的使用步驟
4.1 平移動(dòng)畫(Translate)
設(shè)置方法1:在XML 代碼中設(shè)置
- 步驟1:在
res/anim
的文件夾里創(chuàng)建動(dòng)畫效果.xml
文件
此處路徑為
res/anim/view_animation.xml
- 步驟2:根據(jù) 不同動(dòng)畫效果的語法 設(shè)置 不同動(dòng)畫參數(shù)潦牛,從而實(shí)現(xiàn)動(dòng)畫效果鹃骂。平移動(dòng)畫效果設(shè)置具體如下
view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用<translate /> 標(biāo)簽表示平移動(dòng)畫
<translate xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動(dòng)畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動(dòng)畫持續(xù)時(shí)間(ms),必須設(shè)置罢绽,動(dòng)畫才有效果
android:startOffset ="1000" // 動(dòng)畫延遲開始時(shí)間(ms)
android:fillBefore = “true” // 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài)静盅,默認(rèn)為true
android:fillAfter = “false” // 動(dòng)畫播放完后良价,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài),優(yōu)先于fillBefore值蒿叠,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值明垢,對(duì)fillAfter值無影響,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動(dòng)畫模式市咽,restart代表正序重放痊银,reverse代表倒序回放,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1)施绎,為infinite時(shí)無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器溯革,即影響動(dòng)畫的播放速度,下面會(huì)詳細(xì)講
// 以下參數(shù)是平移動(dòng)畫特有的屬性
android:fromXDelta="0" // 視圖在水平方向x 移動(dòng)的起始值
android:toXDelta="500" // 視圖在水平方向x 移動(dòng)的結(jié)束值
android:fromYDelta="0" // 視圖在豎直方向y 移動(dòng)的起始值
android:toYDelta="500" // 視圖在豎直方向y 移動(dòng)的結(jié)束值
/>
- 步驟3:在Java代碼中創(chuàng)建
Animation
對(duì)象并播放動(dòng)畫
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
// 步驟2:創(chuàng)建 動(dòng)畫對(duì)象 并傳入設(shè)置的動(dòng)畫效果xml文件
mButton.startAnimation(translateAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
效果圖
設(shè)置方法2:在 Java 代碼中設(shè)置
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation translateAnimation = new TranslateAnimation(0,500谷醉,0致稀,500);
// 步驟2:創(chuàng)建平移動(dòng)畫的對(duì)象:平移動(dòng)畫對(duì)應(yīng)的Animation子類為TranslateAnimation
// 參數(shù)分別是:
// 1. fromXDelta :視圖在水平方向x 移動(dòng)的起始值
// 2. toXDelta :視圖在水平方向x 移動(dòng)的結(jié)束值
// 3. fromYDelta :視圖在豎直方向y 移動(dòng)的起始值
// 4. toYDelta:視圖在豎直方向y 移動(dòng)的結(jié)束值
translateAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”,如setDuration()
mButton.startAnimation(translateAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
與第一種設(shè)置方式是一樣的俱尼。
4.2 縮放動(dòng)畫(Scale)
設(shè)置方法1:在XML 代碼中設(shè)置
- 步驟1:在
res/anim
的文件夾里創(chuàng)建動(dòng)畫效果.xml
文件
此處為
res/anim/view_animation.xml
- 步驟2:根據(jù) 不同動(dòng)畫效果的語法 設(shè)置 不同動(dòng)畫參數(shù)抖单,從而實(shí)現(xiàn)動(dòng)畫效果∮霭耍縮放動(dòng)畫效果設(shè)置具體如下:
view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用<scale/> 標(biāo)簽表示是縮放動(dòng)畫
<scale xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動(dòng)畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動(dòng)畫持續(xù)時(shí)間(ms)矛绘,必須設(shè)置,動(dòng)畫才有效果
android:startOffset ="1000" // 動(dòng)畫延遲開始時(shí)間(ms)
android:fillBefore = “true” // 動(dòng)畫播放完后刃永,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài)货矮,默認(rèn)為true
android:fillAfter = “false” // 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài)揽碘,優(yōu)先于fillBefore值次屠,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值园匹,對(duì)fillAfter值無影響,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動(dòng)畫模式劫灶,restart代表正序重放裸违,reverse代表倒序回放,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1)本昏,為infinite時(shí)無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器供汛,即影響動(dòng)畫的播放速度,下面會(huì)詳細(xì)講
// 以下參數(shù)是縮放動(dòng)畫特有的屬性
android:fromXScale="0.0"
// 動(dòng)畫在水平方向X的起始縮放倍數(shù)
// 0.0表示收縮到?jīng)]有;1.0表示正常無伸縮
// 值小于1.0表示收縮涌穆;值大于1.0表示放大
android:toXScale="2" //動(dòng)畫在水平方向X的結(jié)束縮放倍數(shù)
android:fromYScale="0.0" //動(dòng)畫開始前在豎直方向Y的起始縮放倍數(shù)
android:toYScale="2" //動(dòng)畫在豎直方向Y的結(jié)束縮放倍數(shù)
android:pivotX="50%" // 縮放軸點(diǎn)的x坐標(biāo)
android:pivotY="50%" // 縮放軸點(diǎn)的y坐標(biāo)
// 軸點(diǎn) = 視圖縮放的中心點(diǎn)
// pivotX pivotY,可取值為數(shù)字怔昨,百分比,或者百分比p
// 設(shè)置為數(shù)字時(shí)(如50)宿稀,軸點(diǎn)為View的左上角的原點(diǎn)在x方向和y方向加上50px的點(diǎn)趁舀。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.ABSOLUTE。
// 設(shè)置為百分比時(shí)(如50%)祝沸,軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上自身寬度50%和y方向自身高度50%的點(diǎn)矮烹。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.RELATIVE_TO_SELF。
// 設(shè)置為百分比p時(shí)(如50%p)罩锐,軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上父控件寬度50%和y方向父控件高度50%的點(diǎn)奉狈。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.RELATIVE_TO_PARENT
// 兩個(gè)50%表示動(dòng)畫從自身中間開始,具體如下圖
/>
- 步驟3:在Java代碼中創(chuàng)建Animation對(duì)象并播放動(dòng)畫
MainActivity.java
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
// 步驟2:創(chuàng)建 動(dòng)畫對(duì)象 并傳入設(shè)置的動(dòng)畫效果xml文件
mButton.startAnimation(scaleAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
下面展示軸點(diǎn)為(50%,50%) & 軸點(diǎn)為(20%,30%)的情況
軸點(diǎn)為(50%,50%)
軸點(diǎn)為(20%,30%)
設(shè)置方法2:在 Java 代碼中設(shè)置
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation rotateAnimation = new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
// 步驟2:創(chuàng)建縮放動(dòng)畫的對(duì)象 & 設(shè)置動(dòng)畫效果:縮放動(dòng)畫對(duì)應(yīng)的Animation子類為RotateAnimation
// 參數(shù)說明:
// 1. fromX :動(dòng)畫在水平方向X的結(jié)束縮放倍數(shù)
// 2. toX :動(dòng)畫在水平方向X的結(jié)束縮放倍數(shù)
// 3. fromY :動(dòng)畫開始前在豎直方向Y的起始縮放倍數(shù)
// 4. toY:動(dòng)畫在豎直方向Y的結(jié)束縮放倍數(shù)
// 5. pivotXType:縮放軸點(diǎn)的x坐標(biāo)的模式
// 6. pivotXValue:縮放軸點(diǎn)x坐標(biāo)的相對(duì)值
// 7. pivotYType:縮放軸點(diǎn)的y坐標(biāo)的模式
// 8. pivotYValue:縮放軸點(diǎn)y坐標(biāo)的相對(duì)值
// pivotXType = Animation.ABSOLUTE:縮放軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 pivotXValue數(shù)值的點(diǎn)(y方向同理)
// pivotXType = Animation.RELATIVE_TO_SELF:縮放軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 自身寬度乘上pivotXValue數(shù)值的值(y方向同理)
// pivotXType = Animation.RELATIVE_TO_PARENT:縮放軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 父控件寬度乘上pivotXValue數(shù)值的值 (y方向同理)
scaleAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”涩惑,如setDuration()
mButton.startAnimation(scaleAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
與第一種設(shè)置方式是一樣的仁期。
4.3 旋轉(zhuǎn)動(dòng)畫(Rotate)
設(shè)置方法1:在XML 代碼中設(shè)置
- 步驟1:在路徑
res/anim
的文件夾里創(chuàng)建動(dòng)畫效果.xml
文件
此處為
res/anim/view_animation.xml
- 步驟2:根據(jù) 不同動(dòng)畫效果的語法 設(shè)置 不同動(dòng)畫參數(shù),從而實(shí)現(xiàn)動(dòng)畫效果竭恬。旋轉(zhuǎn)動(dòng)畫效果設(shè)置具體如下:
view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用<rotate/> 標(biāo)簽表示是旋轉(zhuǎn)動(dòng)畫
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動(dòng)畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動(dòng)畫持續(xù)時(shí)間(ms)跛蛋,必須設(shè)置,動(dòng)畫才有效果
android:startOffset ="1000" // 動(dòng)畫延遲開始時(shí)間(ms)
android:fillBefore = “true” // 動(dòng)畫播放完后痊硕,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài)问芬,默認(rèn)為true
android:fillAfter = “false” // 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài)寿桨,優(yōu)先于fillBefore值此衅,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值,對(duì)fillAfter值無影響亭螟,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動(dòng)畫模式挡鞍,restart代表正序重放,reverse代表倒序回放预烙,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1)墨微,為infinite時(shí)無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器,即影響動(dòng)畫的播放速度,下面會(huì)詳細(xì)講
// 以下參數(shù)是旋轉(zhuǎn)動(dòng)畫特有的屬性
android:duration="1000"
android:fromDegrees="0" // 動(dòng)畫開始時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針扁掸,負(fù)數(shù) = 逆時(shí)針)
android:toDegrees="270" // 動(dòng)畫結(jié)束時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針翘县,負(fù)數(shù) = 逆時(shí)針)
android:pivotX="50%" // 旋轉(zhuǎn)軸點(diǎn)的x坐標(biāo)
android:pivotY="0" // 旋轉(zhuǎn)軸點(diǎn)的y坐標(biāo)
// 軸點(diǎn) = 視圖縮放的中心點(diǎn)
// pivotX pivotY,可取值為數(shù)字最域,百分比,或者百分比p
// 設(shè)置為數(shù)字時(shí)(如50)锈麸,軸點(diǎn)為View的左上角的原點(diǎn)在x方向和y方向加上50px的點(diǎn)镀脂。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.ABSOLUTE。
// 設(shè)置為百分比時(shí)(如50%)忘伞,軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上自身寬度50%和y方向自身高度50%的點(diǎn)薄翅。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.RELATIVE_TO_SELF。
// 設(shè)置為百分比p時(shí)(如50%p)氓奈,軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上父控件寬度50%和y方向父控件高度50%的點(diǎn)翘魄。在Java代碼里面設(shè)置這個(gè)參數(shù)的對(duì)應(yīng)參數(shù)是Animation.RELATIVE_TO_PARENT
// 兩個(gè)50%表示動(dòng)畫從自身中間開始,具體如下圖
/>
- 步驟3:在Java代碼中創(chuàng)建
Animation
對(duì)象并播放動(dòng)畫
MainActivity.java
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
// 步驟2:創(chuàng)建 動(dòng)畫對(duì)象 并傳入設(shè)置的動(dòng)畫效果xml文件
mButton.startAnimation(scaleAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
設(shè)置方法2:在 Java 代碼中設(shè)置
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation rotateAnimation = new RotateAnimation(0,270,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
// 步驟2:創(chuàng)建旋轉(zhuǎn)動(dòng)畫的對(duì)象 & 設(shè)置動(dòng)畫效果:旋轉(zhuǎn)動(dòng)畫對(duì)應(yīng)的Animation子類為RotateAnimation
// 參數(shù)說明:
// 1. fromDegrees :動(dòng)畫開始時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針舀奶,負(fù)數(shù) = 逆時(shí)針)
// 2. toDegrees :動(dòng)畫結(jié)束時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針暑竟,負(fù)數(shù) = 逆時(shí)針)
// 3. pivotXType:旋轉(zhuǎn)軸點(diǎn)的x坐標(biāo)的模式
// 4. pivotXValue:旋轉(zhuǎn)軸點(diǎn)x坐標(biāo)的相對(duì)值
// 5. pivotYType:旋轉(zhuǎn)軸點(diǎn)的y坐標(biāo)的模式
// 6. pivotYValue:旋轉(zhuǎn)軸點(diǎn)y坐標(biāo)的相對(duì)值
// pivotXType = Animation.ABSOLUTE:旋轉(zhuǎn)軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 pivotXValue數(shù)值的點(diǎn)(y方向同理)
// pivotXType = Animation.RELATIVE_TO_SELF:旋轉(zhuǎn)軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 自身寬度乘上pivotXValue數(shù)值的值(y方向同理)
// pivotXType = Animation.RELATIVE_TO_PARENT:旋轉(zhuǎn)軸點(diǎn)的x坐標(biāo) = View左上角的原點(diǎn) 在x方向 加上 父控件寬度乘上pivotXValue數(shù)值的值 (y方向同理)
rotateAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”,如setDuration()
mButton.startAnimation(rotateAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
與第一種設(shè)置方式是一樣的育勺。
4.4 透明度動(dòng)畫(Alpha)
設(shè)置方法1:在XML 代碼中設(shè)置
- 步驟1:在路徑
res/anim
的文件夾里創(chuàng)建動(dòng)畫效果.xml
文件
此處為
res/anim/view_animation.xml
- 步驟2:根據(jù) 不同動(dòng)畫效果的語法 設(shè)置 不同動(dòng)畫參數(shù)光羞,從而實(shí)現(xiàn)動(dòng)畫效果。旋轉(zhuǎn)動(dòng)畫效果設(shè)置具體如下
view_animation.xml
v<?xml version="1.0" encoding="utf-8"?>
// 采用<alpha/> 標(biāo)簽表示是透明度動(dòng)畫
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動(dòng)畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動(dòng)畫持續(xù)時(shí)間(ms)怀大,必須設(shè)置,動(dòng)畫才有效果
android:startOffset ="1000" // 動(dòng)畫延遲開始時(shí)間(ms)
android:fillBefore = “true” // 動(dòng)畫播放完后呀闻,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài)化借,默認(rèn)為true
android:fillAfter = “false” // 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài)捡多,優(yōu)先于fillBefore值蓖康,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值,對(duì)fillAfter值無影響垒手,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動(dòng)畫模式蒜焊,restart代表正序重放,reverse代表倒序回放科贬,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1)泳梆,為infinite時(shí)無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器,即影響動(dòng)畫的播放速度,下面會(huì)詳細(xì)講
// 以下參數(shù)是透明度動(dòng)畫特有的屬性
android:fromAlpha="1.0" // 動(dòng)畫開始時(shí)視圖的透明度(取值范圍: -1 ~ 1)
android:toAlpha="0.0"http:// 動(dòng)畫結(jié)束時(shí)視圖的透明度(取值范圍: -1 ~ 1)
/>
- 步驟3:在Java代碼中創(chuàng)建
Animation
對(duì)象并播放動(dòng)畫
MainActivity.java
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
// 步驟2:創(chuàng)建 動(dòng)畫對(duì)象 并傳入設(shè)置的動(dòng)畫效果xml文件
mButton.startAnimation(alphaAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
透明度從1-0榜掌,即從有 到 無
設(shè)置方法2:在 Java 代碼中設(shè)置
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation alphaAnimation = new AlphaAnimation(1,0);
// 步驟2:創(chuàng)建透明度動(dòng)畫的對(duì)象 & 設(shè)置動(dòng)畫效果:透明度動(dòng)畫對(duì)應(yīng)的Animation子類為AlphaAnimation
// 參數(shù)說明:
// 1. fromAlpha:動(dòng)畫開始時(shí)視圖的透明度(取值范圍: -1 ~ 1)
// 2. toAlpha:動(dòng)畫結(jié)束時(shí)視圖的透明度(取值范圍: -1 ~ 1)
alphaAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”优妙,如setDuration()
mButton.startAnimation(alphaAnimation);
// 步驟3:播放動(dòng)畫
- 效果圖
與第一種設(shè)置方式是一樣的。
4.5 組合動(dòng)畫
- 上面講的都是單個(gè)動(dòng)畫效果憎账;而實(shí)際中很多需求都需要同時(shí)使用平移套硼、縮放、旋轉(zhuǎn) & 透明度4種動(dòng)畫胞皱,即組合動(dòng)畫
- 使用組合動(dòng)畫需要用到標(biāo)簽
< Set/>
Set
對(duì)于Animation
邪意,就類似View
對(duì)于ViewGroup
- 組合動(dòng)畫同樣有XML 代碼 / Java 代碼兩種設(shè)置方法,下面會(huì)詳細(xì)說明
4.5.1 在XML 代碼中設(shè)置
- 步驟1:在路徑
res/anim
的文件夾里創(chuàng)建動(dòng)畫效果.xml
文件
此處為
res/anim/view_animation.xml
- 步驟2:組合動(dòng)畫的設(shè)置方法 同 單個(gè)動(dòng)畫設(shè)置九妈。具體如下:
view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用< Set/>標(biāo)簽
<set xmlns:android="http://schemas.android.com/apk/res/android">
// 組合動(dòng)畫同樣具備公共屬性
android:duration="3000" // 動(dòng)畫持續(xù)時(shí)間(ms),必須設(shè)置雾鬼,動(dòng)畫才有效果
android:startOffset ="1000" // 動(dòng)畫延遲開始時(shí)間(ms)
android:fillBefore = “true” // 動(dòng)畫播放完后萌朱,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài),默認(rèn)為true
android:fillAfter = “false” // 動(dòng)畫播放完后呆贿,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài)嚷兔,優(yōu)先于fillBefore值,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值做入,對(duì)fillAfter值無影響冒晰,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動(dòng)畫模式,restart代表正序重放竟块,reverse代表倒序回放壶运,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1),為infinite時(shí)無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器浪秘,即影響動(dòng)畫的播放速度,下面會(huì)詳細(xì)講
// 組合動(dòng)畫獨(dú)特的屬性
android:shareinterpolator = “true”
// 表示組合動(dòng)畫中的動(dòng)畫是否和集合共享同一個(gè)差值器
// 如果集合不指定插值器蒋情,那么子動(dòng)畫需要單獨(dú)設(shè)置
// 組合動(dòng)畫播放時(shí)是全部動(dòng)畫同時(shí)開始
// 如果想不同動(dòng)畫不同時(shí)間開始就要使用android:startOffset屬性來延遲單個(gè)動(dòng)畫播放時(shí)間
// 設(shè)置旋轉(zhuǎn)動(dòng)畫,語法同單個(gè)動(dòng)畫
<rotate
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:repeatCount="infinite"
/>
// 設(shè)置平移動(dòng)畫耸携,語法同單個(gè)動(dòng)畫
<translate
android:duration="10000"
android:startOffset = “1000”// 延遲該動(dòng)畫播放時(shí)間
android:fromXDelta="-50%p"
android:fromYDelta="0"
android:toXDelta="50%p"
android:toYDelta="0" />
// 設(shè)置透明度動(dòng)畫棵癣,語法同單個(gè)動(dòng)畫
<alpha
android:startOffset="7000"
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
// 設(shè)置縮放動(dòng)畫,語法同單個(gè)動(dòng)畫
<scale
android:startOffset="4000"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5" />
// 特別注意:
// 1. 在組合動(dòng)畫里scale縮放動(dòng)畫設(shè)置的repeatCount(重復(fù)播放)和fillBefore(播放完后夺衍,視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài))是無效的狈谊。
// 2. 所以如果需要重復(fù)播放或者回到原位的話需要在set標(biāo)簽里設(shè)置
// 3. 但是由于此處rotate旋轉(zhuǎn)動(dòng)畫里已設(shè)置repeatCount為infinite,所以動(dòng)畫不會(huì)結(jié)束沟沙,也就看不到重播和回復(fù)原位
</set>
- 步驟3:在Java代碼中創(chuàng)建Animation對(duì)象并播放動(dòng)畫
MainActivity.java
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
// 步驟2:創(chuàng)建 動(dòng)畫對(duì)象 并傳入設(shè)置的動(dòng)畫效果xml文件
mButton.startAnimation(translateAnimation);
// 步驟3:播放動(dòng)畫
-
效果圖
組合動(dòng)畫
4.5.2 在 Java 代碼中設(shè)置
Button mButton = (Button) findViewById(R.id.Button);
// 創(chuàng)建 需要設(shè)置動(dòng)畫的 視圖View
// 組合動(dòng)畫設(shè)置
AnimationSet setAnimation = new AnimationSet(true);
// 步驟1:創(chuàng)建組合動(dòng)畫對(duì)象(設(shè)置為true)
// 步驟2:設(shè)置組合動(dòng)畫的屬性
// 特別說明以下情況
// 因?yàn)樵谙旅娴男D(zhuǎn)動(dòng)畫設(shè)置了無限循環(huán)(RepeatCount = INFINITE)
// 所以動(dòng)畫不會(huì)結(jié)束河劝,而是無限循環(huán)
// 所以組合動(dòng)畫的下面兩行設(shè)置是無效的
setAnimation.setRepeatMode(Animation.RESTART);
setAnimation.setRepeatCount(1);// 設(shè)置了循環(huán)一次,但無效
// 步驟3:逐個(gè)創(chuàng)建子動(dòng)畫(方式同單個(gè)動(dòng)畫創(chuàng)建方式,此處不作過多描述)
// 子動(dòng)畫1:旋轉(zhuǎn)動(dòng)畫
Animation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
// 子動(dòng)畫2:平移動(dòng)畫
Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT,-0.5f,
TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
TranslateAnimation.RELATIVE_TO_SELF,0
,TranslateAnimation.RELATIVE_TO_SELF,0);
translate.setDuration(10000);
// 子動(dòng)畫3:透明度動(dòng)畫
Animation alpha = new AlphaAnimation(1,0);
alpha.setDuration(3000);
alpha.setStartOffset(7000);
// 子動(dòng)畫4:縮放動(dòng)畫
Animation scale1 = new ScaleAnimation(1,0.5f,1,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scale1.setDuration(1000);
scale1.setStartOffset(4000);
// 步驟4:將創(chuàng)建的子動(dòng)畫添加到組合動(dòng)畫里
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(translate);
setAnimation.addAnimation(scale1);
mButton.startAnimation(setAnimation);
// 步驟5:播放動(dòng)畫
- 效果圖
與第一種設(shè)置方式是一樣的。
5. 監(jiān)聽動(dòng)畫
-
Animation
類通過監(jiān)聽動(dòng)畫開始 / 結(jié)束 / 重復(fù)時(shí)刻來進(jìn)行一系列操作矛紫,如跳轉(zhuǎn)頁面等等 - 通過在 Java 代碼里
setAnimationListener()
方法設(shè)置
Animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// 動(dòng)畫開始時(shí)回調(diào)
}
@Override
public void onAnimationEnd(Animation animation) {
// 動(dòng)畫結(jié)束時(shí)回調(diào)
}
@Override
public void onAnimationRepeat(Animation animation) {
//動(dòng)畫重復(fù)執(zhí)行的時(shí)候回調(diào)
}
});
特別注意
若采取上述方法監(jiān)聽動(dòng)畫赎瞎,每次監(jiān)聽都必須重寫4個(gè)方法。
- 背景:有些時(shí)候我們并不需要監(jiān)聽動(dòng)畫的所有時(shí)刻
- 問題:但上述方式是必須需要重寫4個(gè)時(shí)刻的方法颊咬,這顯示太累贅
- 解決方案:采用動(dòng)畫適配器
AnimatorListenerAdapter
务甥,解決
實(shí)現(xiàn)接口繁瑣 的問題 - 具體如下:
anim.addListener(new AnimatorListenerAdapter() {
// 向addListener()方法中傳入適配器對(duì)象AnimatorListenerAdapter()
// 由于AnimatorListenerAdapter中已經(jīng)實(shí)現(xiàn)好每個(gè)接口
// 所以這里不實(shí)現(xiàn)全部方法也不會(huì)報(bào)錯(cuò)
@Override
public void onAnimationStart(Animator animation) {
// 如想只想監(jiān)聽動(dòng)畫開始時(shí)刻,就只需要單獨(dú)重寫該方法就可以
}
});
6. 應(yīng)用場(chǎng)景
6.1 標(biāo)準(zhǔn)的動(dòng)畫效果
- 補(bǔ)間動(dòng)畫常用于視圖View的一些標(biāo)準(zhǔn)動(dòng)畫效果:平移喳篇、旋轉(zhuǎn)缓呛、縮放 & 透明度;
- 除了常規(guī)的動(dòng)畫使用杭隙,補(bǔ)間動(dòng)畫還有一些特殊的應(yīng)用場(chǎng)景哟绊。
6.2 特殊的應(yīng)用場(chǎng)景
-
Activity
的切換效果 -
Fragement
的切換效果 - 視圖組(
ViewGroup
)中子元素的出場(chǎng)效果
6.2.1 Activity 的切換效果
即 Activity 啟動(dòng) / 退出時(shí)的動(dòng)畫效果。
a.啟動(dòng)動(dòng)畫
Intent intent = new Intent (this,Acvtivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
// 采用overridePendingTransition(int enterAnim, int exitAnim)進(jìn)行設(shè)置
// enterAnim:從Activity a跳轉(zhuǎn)到Activity b,進(jìn)入b時(shí)的動(dòng)畫效果資源ID
// exitAnim:從Activity a跳轉(zhuǎn)到Activity b票髓,離開a時(shí)的動(dòng)畫效果資源Id
// 特別注意
// overridePendingTransition()必須要在startActivity(intent)后被調(diào)用才能生效
b.退出動(dòng)畫
@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
// 采用overridePendingTransition(int enterAnim, int exitAnim)進(jìn)行設(shè)置
// enterAnim:從Activity a跳轉(zhuǎn)到Activity b攀涵,進(jìn)入b時(shí)的動(dòng)畫效果資源ID
// exitAnim:從Activity a跳轉(zhuǎn)到Activity b,離開a時(shí)的動(dòng)畫效果資源Id
// 特別注意
// overridePendingTransition()必須要在finish()后被調(diào)用才能生效
}
對(duì)于參數(shù) enterAnim
& exitAnim
的資源ID洽沟,系統(tǒng)有自帶的效果android.R.anim.xxx
以故,如下設(shè)置:
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivity(intent);
// 淡入淡出的動(dòng)畫效果
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
// 從左向右滑動(dòng)的效果
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
除了使用系統(tǒng)自帶的切換效果,還可以自定義Activity的切換效果:
此處就用到補(bǔ)間動(dòng)畫了
a. 自定義 淡入淡出 效果
淡入淡出 效果是采用透明度動(dòng)畫(Alpha
)裆操。
fade_in.xml(淡入)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
fade_out.xml(淡出)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
在Java代碼中設(shè)置
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivity(intent);
// 自定義的淡入淡出動(dòng)畫效果
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
b. 自定義 左右滑動(dòng) 效果
- 左右滑動(dòng) 效果是采用平移動(dòng)畫(Translate)
-
先了解 Activity 的位置信息怒详,如下圖
位置信息
從上圖可以看出:
- 以屏幕底邊為X軸,屏幕左邊為Y軸踪区;
- 當(dāng)Activity在X軸 = -100%p時(shí)昆烁,剛好完全超出屏幕到左邊(位置1)
- 當(dāng)Activity在X軸 = 0%p時(shí),剛好完全在屏幕內(nèi)(位置2)
- 當(dāng)Activity在X軸 = 100%p時(shí)缎岗,剛好完全超出屏幕到右邊(位置3)
下面自定義一個(gè)動(dòng)畫效果:從右滑到左
out_to_left.xml
從中間滑到左邊静尼,即從位置2 - 位置1
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="500"
android:fromXDelta="0%p"
android:toXDelta="-100%p"
/>
</set>
in_from_right.xml
從右邊滑到中間,即從位置3 - 位置2
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0%p"
/>
</set>
在Java代碼中設(shè)置效果
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivity(intent)
// 自定義 從右向左滑動(dòng)的效果
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
效果圖
- 關(guān)于 縮放和旋轉(zhuǎn)動(dòng)畫 作為Activity的動(dòng)畫效果也是類似的
- 通過 想象力 能組合 上述4種基本動(dòng)畫 進(jìn)行動(dòng)畫效果展示
即這種切換效果還能使用補(bǔ)間動(dòng)畫的組合動(dòng)畫
6.2.2 Fragment動(dòng)畫切換效果
系統(tǒng)自帶的動(dòng)畫切換效果
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setTransition(int transit)传泊;
// 通過setTransition(int transit)進(jìn)行設(shè)置
// transit參數(shù)說明
// 1. FragmentTransaction.TRANSIT_NONE:無動(dòng)畫
// 2. FragmentTransaction.TRANSIT_FRAGMENT_OPEN:標(biāo)準(zhǔn)的打開動(dòng)畫效果
// 3. FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:標(biāo)準(zhǔn)的關(guān)閉動(dòng)畫效果
// 標(biāo)準(zhǔn)動(dòng)畫設(shè)置好后鼠渺,在Fragment添加和移除的時(shí)候都會(huì)有。
自定義動(dòng)畫效果
// 采用`FragmentTransavtion`的 `setCustomAnimations()`進(jìn)行設(shè)置
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(
R.anim.in_from_right,
R.anim.out_to_left);
// 此處的自定義動(dòng)畫效果同Activity眷细,此處不再過多描述
6.2.3 視圖組(ViewGroup
)中子元素的出場(chǎng)效果
- 視圖組(ViewGroup)中子元素可以具備出場(chǎng)時(shí)的補(bǔ)間動(dòng)畫效果
- 常用需求場(chǎng)景:為L(zhǎng)istView的 item 設(shè)置出場(chǎng)動(dòng)畫
- 使用步驟:
步驟1:設(shè)置子元素的出場(chǎng)動(dòng)畫
res/anim/view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 此處采用了組合動(dòng)畫
<set xmlns:android="http://schemas.android.com/apk/res/android" >
android:duration="3000"
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
<translate
android:fromXDelta="500"
android:toXDelta="0"
/>
</set>
步驟2:設(shè)置 視圖組(ViewGroup)的動(dòng)畫文件
res/ anim /anim_layout.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用LayoutAnimation標(biāo)簽
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
// 子元素開始動(dòng)畫的時(shí)間延遲
// 如子元素入場(chǎng)動(dòng)畫的時(shí)間總長(zhǎng)設(shè)置為300ms
// 那么 delay = "0.5" 表示每個(gè)子元素都會(huì)延遲150ms才會(huì)播放動(dòng)畫效果
// 第一個(gè)子元素延遲150ms播放入場(chǎng)效果拦盹;第二個(gè)延遲300ms,以此類推
android:animationOrder="normal"
// 表示子元素動(dòng)畫的順序
// 可設(shè)置屬性為:
// 1. normal :順序顯示溪椎,即排在前面的子元素先播放入場(chǎng)動(dòng)畫
// 2. reverse:倒序顯示普舆,即排在后面的子元素先播放入場(chǎng)動(dòng)畫
// 3. random:隨機(jī)播放入場(chǎng)動(dòng)畫
android:animation="@anim/view_animation"
// 設(shè)置入場(chǎng)的具體動(dòng)畫效果
// 將步驟1的子元素出場(chǎng)動(dòng)畫設(shè)置到這里
/>
步驟3:為視圖組(ViewGroup)指定andorid:layoutAnimation屬性
指定的方式有兩種: XML / Java代碼設(shè)置
- 方式1:在 XML 中指定
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layoutAnimation="@anim/anim_layout"
// 指定layoutAnimation屬性用以指定子元素的入場(chǎng)動(dòng)畫
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
- 方式2:在Java代碼中指定
這樣就不用額外設(shè)置res/ anim /anim_layout.xml該xml文件了
ListView lv = (ListView) findViewById(R.id.listView1);
Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_item);
// 加載子元素的出場(chǎng)動(dòng)畫
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
// 設(shè)置LayoutAnimation的屬性
lv.setLayoutAnimation(controller);
// 為L(zhǎng)istView設(shè)置LayoutAnimation的屬性
上述二者的效果是一樣的。
實(shí)例效果
至此池磁,
Android
動(dòng)畫中的補(bǔ)間動(dòng)畫
的所有知識(shí)點(diǎn)都講解完畢。