2.視圖動畫
文章目錄
2.1 視圖動畫標籤
2.1.1 概述
Android 的視圖動畫由5種類型組成:
- alpha:漸變透明度動畫效果。
- scale:漸變尺寸伸縮動畫效果。
- translate:畫面變換位置移動動畫效果。
- rotate:畫面轉移旋轉動畫效果。
- set:定義動畫集。
舉例:
scale標籤定義XML 動畫文件:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:duration="700"/>
存放位置:
res/anim
;訪問方式:R.anim.XXX
也可以存放在:
res/drawable
;訪問方式:R.drawable.XXX
2.1.2 scale標籤
- fomXScale:動畫起始時,控件在X軸方向上相對自身的縮放比例,浮點值。
- toXScale:動畫結束時,控件在X 軸方向上相對自身的縮放比例,浮點值。
- fomYScale:動畫起始時,控件在Y 軸方向上相對自身的縮放比例,浮點值。
- toYScale : 動畫結束時,控件在Y 軸方向上相對自身的縮放比例,浮點值。
- pivotX:縮放起始點X軸座標,可以是數值、百分數、百分數p三種樣式。
- pivotY:縮放起始點Y 軸座標。
2.1.3 Animation繼承屬性
- duration :用於設置完成一次動畫的持續時間,以毫秒爲單位。
- fillAfter:如果設置爲true,則控件動畫結束時,將保持動畫結束時的狀態。
- fillBefore:如果設置爲true,則控件動畫結束時,將還原到初始化狀態。
- fillEnable:與fillBefore效果相同。
- repeatCount:用於指定動畫的重複次數,當取值爲infinite時,表示無限循環。
- repeatMode :用於設定重複的類型,有reverse 和restart 兩個值。需與repeatCount一起使用。
- interpolator:用於設定插值器,其實就是指定的動畫效果,比如彈跳效果等。
2.1.4 alpha 標籤
- fomAlpha:動畫開始時的透明度,取值範圍爲0.0~1 .0,0.0表示全透明,1.0表示完全不透明。
- toAlpha:動畫結束時的透明度,取值範圍爲0.0~ 1.0,0.0表示全透明,1.0表示完全不透明。
2.1.5 rotate 標籤
- fromDegrees :動畫開始旋轉時的角度位置,正值代表順時針方向的度數,負值代表逆時針方向的度數。
- toDegrees :動畫結束時旋轉到的角度位置,正值代表順時針方向的度數,負值代表逆時針方向的度數。
- pivotX: 旋轉中心點X軸座標,默認旋轉中心點是控件座標原點。可以是數值、百分數、百分數p 三種樣式。
- pivotY: 旋轉中心點Y軸座標。
2.1.6 translate 標籤
- fromXDelta:起始點X軸座標,可以是數值、百分數、百分數p三種樣式。
- fromYDelta:起始點Y軸座標。
- toXDelta:終點X軸座標。
- toYDelta:終點Y軸座標。
2.1.7 set 標籤
- set標籤是一個容器類標籤,用於定義動畫集。
- 注意:在set標籤中設置repeateCount屬性是無效的,必須對每個動畫單獨設直纔有作用。
2.2 視圖動畫的代碼實現
2.2.1 概述
- 標籤與所對應的類如下表所示:
標籤 | 類 |
---|---|
scale | ScaleAnimation |
alpha | AlphaAnimation |
rotate | RotateAnimation |
translate | TranslateAnimation |
set | AnimationSet |
- Animation類裏的標籤屬性與方法的對應關係:
標籤屬性 | 方法 |
---|---|
duration | setDuration(long) |
fiIIAfter | setFillAfter(boolean) |
fillBefore | setFillBefore(boolean) |
fillEnabled | setFillEnabled(boolean) |
repeatCount | setRepeatCount(int) |
repeatMode | setRepeatMode(int) |
interpolator | setlnterpolator(lnterpolator) |
2.2.2 ScaleAnimation
ScaleAnimation(Context context , AttributeSet attrs) ScaleAnimation(float fromX , float toX, float fromY , float toY) ScaleAnimation(float fromX , float toX , float fromY , float toY , float pivotX, float pivotY) ScaleAnimation(float fromX , float toX, float fromY , float toY, intpivotXType, float pivotXValue , int pivotYType , float pivotYValue)
pivotXType:
- Animation.ABSOLUTE
- Animation.RELATIVE_TO_SELF
- Animation.RELATIVE_TO_PARENT
2.2.3 AlphaAnimation
AlphaAnimation(Context context , AttributeSet attrs) AlphaAnimation(float fromAlpha , float toAlpha)
2.2.4 RotateAnimation
RotateAnimation(Context context , AttributeSet attrs) RotateAnimation(float fromDegrees , float toDegrees) RotateAnimation(float fromDegrees , float toDegrees , float pivotX , float pivotY) RotateAnimation(float fromDegrees , float toDegrees , int pivotXType , float pivotXValue , int pivotYType , float pivotYValue)
2.2.5 TranslateAnimation
TranslateAnimation(Context context , AttributeSet attrs) TranslateAnimation(float fromXDelta , float toXDelta , float fromYDelta , float toYDelta) TranslateAnimation(int fromXType , float fromXValue , int toXType , float toXValue , int fromYType , float fromYValue , int toYType , float toYValue)
2.2.6 AnimationSet
AnimationSet(Context context , AttributeSet attrs) AnimationSet(boolean shareInterpolator)
shareInterpolator參數的取值有兩個: true 和false 。
- true:用於在AnimationSet類中定義一個插值器,其下面的所有動畫共用該插值器。
- false:表示其下面的動畫定義各自的插值器。
增加動畫的函數:
public void addAnimation(Animation a)
2.2.7 Animation
一些函數:
void cancel() //取消動畫。 void reset() //將控件重置到動畫開始前狀態。 void setAnimationListener(Animation.AnimationListener listener)//設置動畫監昕 //Animation.AnimationListener中的回調函數: abstract void onAnimationEnd(Animation animation) abstract void onAnimationRepeat(Animation animation) abstract void onAnimationStart(Animation animation)
2.3 差值器簡介
有關動畫的變化速率的問題是由Interpolator類(插值器)來決定的。
Interpolator是一個接口,通過實現這個接口就可以自定義動畫的變化速率。
系統提供瞭如幾個己經實現了插值器的類:
Interpolator class Resource ID AccelerateDecelerateinterpolator @android:anim/accelerate_decelerate_interpolator AccelrateInterpolator @android:anim/accelerate_interpolator AnticipateInterpolator @android:anim/anticipate_interpolator AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator BounceInterpolator @android:anim/bounce_interpolator CycleInterpolator @android:anim/cycle_interpolator DecelerateInterpolator @android:anim/decelerate_interpolator LinearInterpolator @android:anim/linear_interpolator OvershootInterpolator @android:anim/overshoot_interpolator 在XML文件中引用差值器:
android:interpolator="@android:anim/accelerate_interpolator"
通過setlnterpolator()函數設置插值器。
2.3.1 差值器說明
AccelerateDeceleratelnterpolator:加速減速插值器。
Acceleratelnterpolator:加速插值器。
Deceleratelnterpolator:減速插值器。
Linearlnterpolator:線性插值器,也稱勻速加速器。
Bouncelnterpolator:彈跳插值器。
Anticipatelnterpolator:初始偏移插值器。
/** * 張力值:默認值爲2,值越大,初始的偏移量越大,而且速度越快。 */ //XML屬性 android:tension //函數 public Anticipateinterpolator(float tension)
Overshootlnterpolator:結束偏移插值器。
/** * 張力值:默認值爲2,值越大,結束的偏移量越大,而且速度越快。 */ //XML屬性 android:tension //函數 public Overshootinterpolator(float tension)
AnticipateOvershootlnterpolator:Anticipatelnterpolator與Overshootlnterpolator的合體,即在動畫開始時向前偏移一段距離,在動畫結束時向後偏移一段距離。
/** * tension張力值:默認值爲2,值越大,起始和結束時的偏移量越大,而且速度越快。 * extraTension額外張力值:默認值爲1.5。 * T = tension * extraTension */ //XML屬性 android:tension android:extraTension //函數 public AnticipateOvershootlnterpolator(float tension) public AnticipateOvershootlnterpolator(float tension, float extraTension)
Cyclelnterpolator:是循環插值器,表示動畫循環播放特定的次數,速率沿正弦曲線改變。
public Cycleinterpolator(float cycles)
技巧:
//延遲各個動畫的開始時間,使各個動畫可以相互間隔。 Animation.setStartOffset(int time);
2.4 逐幀動畫
- 謂逐幀動畫(Frame Animation),就是一幀接着一幀地播放圖片。
2.4.1 XML實現
**1.定義XML 動畫文件:**定義在
res/drawable
下<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "false"]> <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" />
- 元素是必需的,並且必須作爲根元素,可以包含一個或多個元素。
- oneshot :true:動畫只會執行1次;false:一直循環。
- 元素代表一幀動畫,android:drawable指定此幀動畫所對應的圖片資源,android :duration代表此幀動畫持續的時間,是一個整數,單位ms。
2.設置ImageView
- 可以通過android:src實現,也可以通過android :background實現。
3.AnimationDrawable 開始動畫
ImageView image = (ImageView)findViewById(R.id.frame_image); AnimationDrawable anim = (AnimationDrawable)image.getDrawable(); anim.start();
- 通過android:src設置動畫資源時,取出方式是image.getDrawable()。
- 通過android:background設置動畫資源,取出方式是image.getBackground()。
AnimationDrawable 類:
- 逐幀動畫需要得到AnimationDrawable類的支持,它是Drawable的間接子類。
- void start():開始播放逐幀動畫。
- void stop():停止播放逐幀動畫。
- int getDuration(int index):得到指定index的幀的持續時間。
- Drawable getFrame(int index):得到指定index 的幀所對應的Drawable 對象。
- int getNumberOfFrames():得到當前AnimationDrawable的所有幀數量。
- boolean isRunning():判斷當前AnimationDrawable是否正在播放。
- void setOneShot(boolean oneShot):設置AnimationDrawable是執行一次還是循環播放。
- boolean isOneShot():判斷當前AnimationDrawable執行一狀態。
- void addFrame(Drawable frame,int duration):爲AnimationDrawable添加1幀,並設置持續時間。
2.4.2 代碼實現
舉例:
ImageView image = (ImageView)findViewById(R.id.frame_image); final AnimationDrawable anim = new AnimationDrawable(); for(int i=1;i<=14;i++){ int id = getResources().getIdentifier("list_icon_gif_playing"+i,"drawable",getPackageName()); Drawable drawable = getResources().getDrawable(id); anim.addFrame(drawable,60); anim.setOneShot(false); image.setBackgroundDrawable(anim); anim.start();
函數說明:
int getidentifier(String name,String defType,String defPackage)
- String name: 所要查找資源ID的資源名稱。
- String defType:資源所在的文件類型。[eg.drawable/string/array]
- String defPackage:應用包名。
參考資料
- 《Android自定義控件開發入門與實戰》