android之animation(一)

animations:動畫。

新知識點介紹:animations是一系列的動畫效果,這些效果可以運用在絕大數控件上面。

animations總的說來可以分爲兩類:

1、Tweened Animations:包括旋轉、移動、伸展、淡入淡出等效果。

2、Frame-by-Frame Animations:這個可以創建一個Drawable序列,可以一個一個的顯示。


這次介紹第一類動畫效果:

這些動畫有一些公共的函數如下:

setDuration(float time)動畫執行時間;

setFillAfter(boolean b)是否保持動畫結束時狀態;

setFillBefore(boolean b)是否返回動畫開始狀態,當動畫結束後;

setStartOffset(long time)經過多少時間動畫開始執行;

setRepeatCount(int i)動畫重複次數(默認爲0)。


設置動畫有兩種方法,直接在代碼中設置;或者在xml中設置。第一種方法比較容易理解,符合開發人員習慣;第二中方法比較容易進行二次開發,代碼少。


一、代碼中設置:

1、rotate:旋轉。表示一個控件繞着一個點做旋轉(平面旋轉)。這個旋轉是圍繞一個點,而這個點是由百分比決定的,比如相對於自身時x爲0.5,y爲0.5,那麼這個點就是該控件的中心;如果x爲1,y爲1,那麼這個點就是控件的右下角;相對父控件時x爲0.5,y爲0.5,那麼這個點就是該父控件的中心。

看代碼:


                	/**
			 * 參數:1、開始角度,0是開始位置;2、最後終止位置與開始位置角度
			 * 3、旋轉點x相對哪個控件,4、x所佔該控件的比例;5、旋轉點y相對哪個控件;6、y所佔該控件的比例
			 */
			RotateAnimation rotateanimation = new RotateAnimation(
					//從現在狀態繞着他本身的右下角旋轉90度
					0, 90, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,1f);
			//設置執行時間
			rotateanimation.setDuration(1000);
			//是否保持動畫完時的狀態
			rotateanimation.setFillAfter(true);
			//是否動畫完了返回原狀態
			rotateanimation.setFillBefore(false);
			//隔多長時間動畫顯示
			rotateanimation.setStartOffset(1000);
			//動畫重複執行次數
			rotateanimation.setRepeatCount(1);
			//設置動畫效果
			imageview.setAnimation(rotateanimation);

 

代碼已經有了具體的註釋,imageview爲一張圖片,上面是將圖片繞着他自己的右下角旋轉90度。

其中新建rotateanimation 時裏面的第3和第5個參數也可以是Animation.RELATIVE_TO_PARENT,

相對父控件的x,y軸比例。


2、alpha:淡入淡出。

代碼:


AlphaAnimation alphaanimation = new AlphaAnimation(1, 0);
			//設置動畫時間
			alphaanimation.setDuration(1000);
imageview.setAnimation(alphaanimation);

 

構造函數的參數:第一個是動畫前的透明度,1表示不透明,0表示完全透明;第二個是動畫後的透明度。


3、translate:移動。移動就是由某一位置移動到另一位置,就是該控件相對自己或者父控件的一位置移動到另一個相對自己或者父控件的位置,那麼他的構造函數就應該能猜到了吧,下面是代碼:



                        /**
			 * 參數:1、移動前x相對的控件,2、x的比例,3、移動後x相對的控件,4、x的比例;
			 * 	     5、移動前y相對的控件 ,6、y的比例,7、移動後y相對的控件,8、y的比例
			 */
			TranslateAnimation translateanimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF,0f,
					Animation.RELATIVE_TO_SELF,1f,
					Animation.RELATIVE_TO_SELF,0f,
					Animation.RELATIVE_TO_SELF,1f);
			translateanimation.setDuration(1000);
			imageview.setAnimation(translateanimation);

 

這個代碼實現的是將該圖片從相對自己的(0,0)座標移動到相對自己的(x的長,y的長)座標處。


4、scale:伸展。對控件進行放大或者縮小。



                         /**
			 * 參數:1、伸展前x的比例,2、伸展後x的比例,3、伸展前y的比例,4、伸展後y的比例
			 * 5、伸展x對應的控件,6、伸展y對應的控件
			 */
			ScaleAnimation scaleanimation  = new ScaleAnimation(
					1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
			scaleanimation.setDuration(1000);
			imageview.setAnimation(scaleanimation);

 

     這個代碼是將該圖片縮小到0。參數都有詳盡的解釋。





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



第二種方法:

在xml中設置:


1、首先在res下新建一個文件夾anim(animation的縮寫)。


2、在該文件夾下新建所需要的動畫的xml:


a、scale .xml:


 

<?xml version="1.0" encoding="utf-8"?>
<set  xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">
	<scale 
		android:fromXScale="1"
		android:toXScale="0"
		android:fromYScale="1"
		android:toYScale="0"
		android:duration="1000"
	/>
</set>

 

 

代碼解釋:scale ,伸展。參數依次是 x的原始百分比;伸展後x的百分比;y的原始百分比;伸展後y的百分比;

動畫時間。


b、alpha,淡入淡出。


<?xml version="1.0" encoding="utf-8"?>

<set  xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">
	<alpha 
		android:fromAlpha="1"
		android:toAlpha="0"
		android:duration="1000"
	/>
</set>


代碼解釋:

控件開始狀態透明度;控件最終透明度;動畫時間。


c、rotate、旋轉。


 

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

 


代碼解釋:

開始角度;最終角度;旋轉點x的百分比;旋轉y的百分比;動畫時間。

這裏要說下,第三個和第四個參數配置有三中:

android:pivotX="50",這個是絕對位置,相對於整個屏幕的位置。

android:pivotX="50%",這個是相對自身的比例。

android:pivotX="50%p",這個是相對父控件的比例。


d、translate : 移動。


 

<?xml version="1.0" encoding="utf-8"?>
<set  xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">
	<translate 
		android:fromXDelta="50%"
		android:toXDelta="50%"
		android:fromYDelta="50%"
		android:toYDelta="50%"
		android:duration="1000"
	/>
</set>

 

 

代碼解釋:


移動前x的位置;移動後x的位置;移動前y的位置;移動後y的位置;時間

他的這個配置和上面的rotate一樣,也有三種方法。不再贅述。


3、在代碼中使用xml:

由於使用所有的xml都類似,我們只列舉一種:


Animation translateanimation = AnimationUtils.loadAnimation(
					Animation1Activity.this, R.anim.translate);
			imageview.setAnimation(translateanimation);

 

代碼解釋:

首先新建一個Animation ,他是由AnimationUtils中的一個靜態方法loadAnimation來實例化,可以通過他們的

名字就知道他們的意思。

這個loadAnimation需要兩個參數,一個是context;一個是這個動畫對應的xml的id。



今天就到這裏了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章