1. 程式人生 > >android動畫一·簡單瞭解

android動畫一·簡單瞭解


動畫

幀動畫

一張張圖片不斷的切換,形成動畫效果

  • 在drawable目錄下定義xml檔案,子節點為animation-list,在這裡定義要顯示的圖片和每張圖片的顯示時長
	<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
		<item android:drawable="@drawable/g1" android:duration="200" />
		<item android:
drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> </animation-list>
  • 在螢幕上播放幀動畫
	ImageView iv = (ImageView) findViewById(R.id.iv);
	//把動畫檔案設定為imageView的背景
    iv.setBackgroundResource(R.drawable.animations);
    AnimationDrawable ad =
(AnimationDrawable) iv.getBackground(); //播放動畫 ad.start();

補間動畫

  • TweenAnimation
  • 指定物件的初始狀態和結束狀態,物件由初始狀態向結束狀態轉變時,為了時變換過程更自然平緩而自動生成的動畫效果
  • 補間動畫不會改變元件物件屬性的值,只是一個動畫效果
  • 位移、旋轉、縮放、透明

位移:

  • 引數10指的是X的起點座標,但不是指螢幕x座標為10的位置,而是imageview的 真實X + 10
  • 引數150指的是X的終點座標,它的值是imageview的 真實X + 150
		//建立為位移動畫物件,設定動畫的初始位置和結束位置
		TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
  • x座標的起點位置,如果相對於自己,傳0.5f,那麼起點座標就是 元件X + 0.5 * 元件寬度
  • x座標的終點位置,如果傳入2,那麼終點座標就是 元件X + 2 * 元件寬度
  • y座標的起點位置,如果傳入0.5f,那麼起點座標就是 元件Y + 0.5 * 元件高度
  • y座標的終點位置,如果傳入2,那麼終點座標就是 元件Y + 2 * 元件高度
		TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
  • 動畫播放相關的設定
		//設定動畫持續時間
    	ta.setDuration(2000);
		//動畫重複播放的次數
    	ta.setRepeatCount(1);
		//動畫重複播放的模式
    	ta.setRepeatMode(Animation.REVERSE);
		//動畫播放完畢後,元件停留在動畫結束的位置上
    	ta.setFillAfter(true);
		//播放動畫
    	iv.startAnimation(ta);

縮放:

  • 引數0.1f表示動畫的起始寬度是真實寬度的0.1倍
  • 引數4表示動畫的結束寬度是真實寬度的4倍
  • 縮放的中心點在iv左上角
		ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
  • 引數0.1f和4意義與上面相同
  • 改變縮放的中心點:傳入的兩個0.5f,型別都是相對於自己,這兩個引數改變了縮放的中心點
  • 中心點x座標 = 元件X + 0.5 * 元件寬度
  • 中心點Y座標 = 元件Y + 0.5 * 元件高度
		ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

透明:

  • 0為完全透明,1為完全不透明
		AlphaAnimation aa = new AlphaAnimation(0, 0.5f);

旋轉:

  • 20表示動畫開始時的iv的角度
  • 360表示動畫結束時iv的角度
  • 預設旋轉的圓心在iv左上角
		RotateAnimation ra = new RotateAnimation(20, 360);
  • 20,360的意義和上面一樣
  • 指定圓心座標,相對於自己,值傳入0.5,那麼圓心的x座標:元件X + iv寬度 * 0.5
  • 圓心的Y座標:元件Y + iv高度 * 0.5
		RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

所有動畫一起

		//建立動畫集合
		AnimationSet set = new AnimationSet(false);
		//往集合中新增動畫
		set.addAnimation(aa);
		set.addAnimation(sa);
		set.addAnimation(ra);
		iv.startAnimation(set);

屬性動畫

  • 改變了元件物件的屬性的值

位移:

  • 第一個引數target指定要顯示動畫的元件
  • 第二個引數propertyName指定要改變元件的哪個屬性
  • 第三個引數values是可變引數,就是賦予屬性的新的值
  • 傳入0,代表x起始座標:當前x + 0
  • 傳入100,代表x終點座標:當前x + 100
		//具有get、set方法的成員變數就稱為屬性
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;

縮放:

  • 第三個引數指定縮放的比例
  • 0.1是從原本高度的十分之一開始
  • 2是到原本高度的2倍結束
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);

透明:

  • 透明度,0是完全透明,1是完全不透明
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);

旋轉

  • rotation指定是順時針旋轉
  • 20是起始角度
  • 270是結束角度
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
  • 屬性指定為rotationX是豎直翻轉
  • 屬性指定為rotationY是水平翻轉
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);

可變引數

  • 第三個引數可變引數可以傳入多個引數,可以實現往回位移(旋轉、縮放、透明)
		ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;

所有動畫一起

		//建立動畫師集合
		AnimatorSet set = new AnimatorSet();
		//設定要播放動畫的元件
		set.setTarget(bt);
		//所有動畫有先後順序的播放
		//set.playSequentially(oa, oa2, oa3, oa4);
		//所有動畫一起播放
		set.playTogether(oa, oa2, oa3, oa4);
		set.start();