在安卓系統中,為了界面或其中的組件在切換、改變的時候顯得自然生動、具有活動性的美感,就給它們添加了動畫的效果。
例如圖片切換的時候,前1張圖片淡出,后1張圖片淡入。
動畫分了3類: frame動畫(逐幀動畫)、 property動畫(屬性動畫)、 tween動畫(漸變動畫)。
逐幀動畫有點像播放電影,它把很多圖片串起來,依照順序1張1張顯示,通過播放構成動畫效果;
屬性動畫是對控件某個屬性使用的動畫,例如1個按鈕的寬度要從窄設置到寬,而我們希望它的寬度調劑的時候,能看到它從窄到寬變化的進程,這時候就需要使用屬性動畫。
漸變動畫是對控件整體使用的動畫,有4種最多見到的效果:透明、平移、縮放和旋轉。
/*******************************************************************/
* 版權聲明
* 本教程只在CSDN和安豆網發布,其他網站出現本教程均屬侵權。
/*******************************************************************/
漸變動畫有4種最多見到的效果:透明、平移、縮放和旋轉。這4種效果都有共同點,
動畫效果可以通過資源文件定義,也能夠通過代碼來定義。
透明動畫的效果是,動畫對象的透明度從1個程度變成另外一個程度。例如從透明變成不透明這類淡入效果。
通過xml定義透明動畫,
res
下新建1個anim目錄
,然后在這個目錄下創建1個xml文件
;定義動畫效果,
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1500"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
透明動畫的各個屬性代表了以下的含義:
<alpha/>
標簽:這是1個透明效果的動畫;fromAlpha
:動畫開始時,動畫對象的透明度值,取值在0⑴
之間,0是完全透明,1是完全不透明,0
和1
之間的值就是各種半透效果;toAlpha
:動畫結束時,動畫對象的透明度值,取值在0⑴
之間;duration
:動畫延續的時間,單位是毫秒;interpolator
:這是插值器,用來指定動畫變化的速率,例如是先快后慢,還是先慢后快,或是均勻的變化。安卓系統為我們提供了很多現成的插值器,我們可以直接使用,例如上面的線性插值器@android:anim/linear_interpolator
;
淡入效果就是剛開始沒有顯示,逐步顯示出來,所以它的fromAlpha
就要設置成0
,toAlpha
設置成1
;
淡出效果就是剛開始顯示,逐步隱藏起來,所以它的fromAlpha
就要設置成1
,toAlpha
設置成0
;
另外,Android SDK也提供了自定義插值器的方法,我們在以后介紹。
通過java代碼定義透明動畫,
AlphaAnimation anim = new AlphaAnimation(0, 1);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
平移動畫的效果是,動畫對象沿著水平方向或垂直方向移動(或兩個方向同時移動)到另外一位置。
通過xml定義透明動畫,
res
下新建1個anim目錄
,然后在這個目錄下創建1個xml文件
;定義動畫效果,
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:fromYDelta="-100%"
android:toXDelta="0%"
android:toYDelta="0%"
android:duration="1500"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
平移動畫的各個屬性代表了以下的含義:
<translate/>
標簽:這是1個平移效果的動畫;fromXDelta
:動畫開始時,動畫對象相對它真實位置在x方向上的坐標;fromYDelta
:動畫開始時,動畫對象相對它真實位置在y方向上的坐標;toXDelta
:動畫結束時,動畫對象相對它真實位置在y方向上的坐標;toYDelta
:動畫結束時,動畫對象相對它真實位置在y方向上的坐標;duration
:動畫延續的時間,單位是毫秒;interpolator
:這是插值器,用來指定動畫變化的速率;起始坐標有3種表達方式:
動畫對象相對本身的位置:用%
表示。例如給fromXDelta
設定-100%
,表示動畫開始時,起始的左側邊界相對本身真實位置,在負的本身寬度的地方;
動畫對象相對它父布局的位置:用%p
表示,這里的p
就是parent
的縮寫。
讓對象從左往右平移進入的效果,就是讓它的fromXDelta
設置成-100%
,toXDelta
設置成0%
;fromYDelta
設置成0%
;toYDelta
設置成0%
;
讓對象從下往上平移進入的效果,就是讓它的fromXDelta
設置成0%
,toXDelta
設置成0%
;fromYDelta
設置成100%
(y坐標是從上到下為正方向);toYDelta
設置成0%
;
通過java代碼定義平移動畫,
Animation.RELATIVE_TO_SELF
表示使用相對本身的位置坐標,Animation.RELATIVE_TO_PARENT
表示使用相對父布局的位置坐標,Animation.ABSOLUTE
表示使用絕對位置坐標;//使用坐標的絕對位置創建動畫
TanslateAnimation anim = new TanslateAnimation(-150, 0, -50, 0);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
//使用坐標的相對位置創建動畫
TanslateAnimation anim = new TanslateAnimation(Animation.RELATIVE_TO_SELF, -1.0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0, Animation.RELATIVE_TO_SELF, 0);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
伸縮動畫的效果是,動畫對象以某個位置為中心進行放大或縮小。
通過xml定義伸縮動畫,
res
下新建1個anim目錄
,然后在這個目錄下創建1個xml文件
;定義動畫效果,
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="-100%"
android:fromYScale="-100%"
android:toXScale="0%"
android:toYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1500"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
伸縮動畫的各個屬性代表了以下的含義:
<scale/>
標簽:這是1個伸縮效果的動畫;fromXScale
:動畫開始時,動畫對象在x方向上相對它真實大小的縮放比例;fromYScale
:動畫開始時,動畫對象在x方向上相對它真實大小的縮放比例;toXScale
:動畫結束時,動畫對象在y方向上相對它真實大小的縮放比例;toYScale
:動畫結束時,動畫對象在y方向上相對它真實大小的縮放比例;duration
:動畫延續的時間,單位是毫秒;interpolator
:這是插值器,用來指定動畫變化的速率;pivotX
:動畫進行縮放時的中心點x坐標;pivotY
:動畫進行縮放時的中心點y坐標;這里的pivotX
與pivotY
可以設置絕對坐標,也能夠設置相對本身的相對坐標%
,和相對父布局的相對坐標%p
。
通過java代碼定義伸縮動畫,
Animation.RELATIVE_TO_SELF
表示使用相對本身的位置坐標,Animation.RELATIVE_TO_PARENT
表示使用相對父布局的位置坐標,Animation.ABSOLUTE
表示使用絕對位置坐標;//使用中心點的絕對位置創建動畫
ScaleAnimation anim = new ScaleAnimation(1, 2, 1, 2, 50, 150);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
//使用中心點的相對位置創建動畫
ScaleAnimation anim = new ScaleAnimation(1, 2, 1, 2, Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0.5);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
旋轉動畫的效果是,動畫對象以某個位置為中心進行旋轉。
通過xml定義旋轉動畫,
res
下新建1個anim目錄
,然后在這個目錄下創建1個xml文件
;定義動畫效果,
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1500"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
伸縮動畫的各個屬性代表了以下的含義:
<rotate/>
標簽:這是1個旋轉效果的動畫;fromDegrees
:動畫開始時,動畫對象相對真實位置旋轉的角度;toDegrees
:動畫開始時,動畫對象相對真實位置旋轉的角度;duration
:動畫延續的時間,單位是毫秒;interpolator
:這是插值器,用來指定動畫變化的速率;pivotX
:動畫進行縮放時的中心點x坐標;pivotY
:動畫進行縮放時的中心點y坐標;這里的pivotX
與pivotY
可以設置絕對坐標,也能夠設置相對本身的相對坐標%
,和相對父布局的相對坐標%p
。
通過java代碼定義旋轉動畫,
Animation.RELATIVE_TO_SELF
表示使用相對本身的位置坐標,Animation.RELATIVE_TO_PARENT
表示使用相對父布局的位置坐標,Animation.ABSOLUTE
表示使用絕對位置坐標;//使用中心點的絕對位置創建動畫
RotateAnimation anim = new RotateAnimation(0, 360, 50, 150);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
//使用中心點的相對位置創建動畫
ScaleAnimation anim = new ScaleAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0.5);
anim.setDuration(1500);
anim.setInterpolator(context, android.R.interpolator.linear);
AnimationUtils.loadAnimation()
裝載動畫;startAnimation()
函數,啟動動畫;Animation anim = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
mAnimationTarget.startAnimation(anim);
使用代碼定義的動畫就和使用xml1樣,對要使用動畫的對象使用startAnimation()
函數,啟動動畫;
mAnimationTarget.startAnimation(anim);
這幾種動畫不但可以單獨使用,也能夠混合著使用,例如你希望1個對象1邊淡入1邊做平移。
對同1個對象同時使用多個動畫效果,只要在定義動畫的xml文件中,添加多個希望的動畫就好了。
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
....../>
<scale
....../>
<translate
....../>
<alpha
....../>
</set>
在java代碼中可以,
TranslateAnimation anim1 = new TranslateAnimation(...);
ScaleAnimation anim2 = new ScaleAnimation(...);
AlphaAnimation Anim3 = new AlphaAnimation(...);
RotateAnimation anim4 = new RotateAnimation(...);
AnimationSet set = new AnimationSet(true);
set.addAnimation(anim1);
set.addAnimation(anim2);
set.addAnimation(anim3);
set.addAnimation(anim4);
mAnimationTarget.startAnimation(set);
有的時候,需要知道1個動畫履行的狀態,就需要給動畫添加1個監聽器。通過監聽器取得動畫狀態變化的通知。
TranslateAnimation anim = new TranslateAnimation(...);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動畫開始
}
@Override
public void onAnimationEnd(Animation animation) {
//動畫結束
}
@Override
public void onAnimationRepeat(Animation animation) {
//動畫重復履行
}
});
在前面我們已介紹了android:duration
android:interpolator
等共同的屬性。現在我們再介紹幾個其它的重要屬性。
android:fillAfter
假定1個按鈕被使用透明效果的動畫,從不透明變到全透明。動畫完成后,這個按鈕會被還原到它的真實狀態(按鈕并沒有消失或透明,依然顯示在之前的位置)。
如果給這個動畫設置了android:fillAfter
屬性為true
,那末在履行完動畫后,按鈕還是保持動畫最后1幀的效果。
這兩個屬性要設置在<set/>
標簽當中才能有效果。
<?xml version="1.0" encoding="utf⑻"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false">
......
</set>
也就是說,如果是用代碼創建動畫的話,得把這個動畫放到AnimationSet
當中再來設置屬性。
RotateAnimation anim = new RotateAnimation(...);
AnimationSet set = new AnimationSet(true);
set.addAnimation(anim);
set.setFillAfter(true);
android:repeatMode
與android:repeatCount
可以通過設置android:repeatCount
的數值,讓動畫重復播放;
0
:動畫重復該數值表示的次數;INFINITE
:動畫將會無窮循環進行下去;當android:repeatCount
被設置大于0
以后,還可以配合使用android:repeatMode
,
RESTART
:每次動畫履行完成后,回到最初狀態重新履行下1次動畫;REVERSE
:每次動畫履行完成后,動畫倒著履行;