1. 程式人生 > >android之animation(二)animationset、interpolator

android之animation(二)animationset、interpolator

一:

animationset:

他是一個animation的一個子類,實際上是animation的一個集合。他將animation放到一個list集合中。需要對animation的基本設定可以通過animationset來設定。如果需要對一個控制元件進行多種動畫設定,可以採用animationset。

程式碼:

 

	              //建立一個animationset
			AnimationSet animationset = new AnimationSet(true);
			//建立一個alphaanimation
			Animation alphaanimation = AnimationUtils.loadAnimation(
					Animation1Activity.this,R.anim.alpha);
			ScaleAnimation scaleanimation  = new ScaleAnimation(
					1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
			//設定動畫時間
			animationset.addAnimation(alphaanimation);
			animationset.addAnimation(scaleanimation);
			animationset.setDuration(2000);
			animationset.setStartOffset(2000);
			imageview.setAnimation(animationset);

 

 

程式碼說明:我們運用AnimationUtils載入一個xml中的alphaanimation,然後向上轉系為Animation,然後再新建一個

scaleanimation,然後通過animationset將這兩個動畫新增到一起,然後繫結在一個控制元件上面。

可以通過只設置animationset來對動畫共有屬性進行配置。

 

****************************************************************************************************

 

二:

interpolator:動畫變化的速率。

在animations框架當下定義了以下幾種interpolator:

AccelerateDecelerateInterpolator  在動畫開始與結束的地方速率改變比較慢,在中間的時候較快。
AccelerateInterpolator  在動畫開始的時候改變較慢,然後開始加速。
CycleInterpolator  動畫迴圈播放特定次數,速率改變沿著正弦曲線。
DecelerateInterpolator   在動畫開始的時候叫慢,然後開始減速。
LinearInterpolator   動畫以均勻速率改變。

 

這個是在xml中進行設定:

 

<?xml version="1.0" encoding="utf-8"?>
<set  xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator"
	android:shareInterpolator="true">
	<alpha 
		android:fromAlpha="1"
		android:toAlpha="0"
		android:duration="1000"
	/>
	<rotate 
		android:fromDegrees="0"
		android:toDegrees="180"
		android:pivotX="100%"
		android:pivotY="100%"
		android:duration="1000"
	/>
</set>
 

程式碼解釋:基本語法已經在上集講過了,現在看上面的那個<set 標籤,表示的就是animationset,指一系列的animation,下面可以加上多個animation。

android:interpolator這個就是設定動畫改變速率,下面的那個android:shareInterpolator指下面的animation是否共享上面的動畫改變速率,如果不共享,可以在下面的animation中單獨設定。