Android基本動畫小結

來源:互聯網
上載者:User

標籤:

    Android提供了兩類動畫,第一類是Tween動畫,就是對情境裡的對象不斷的進行映像變化來產生動畫效果(旋轉、平移、放縮和漸層)。第二類就是 Frame動畫,即順序的播放事先做好的映像。

 

1. Tween動畫

主要類:

Animation           動畫

AlphaAnimation    漸層透明度

RotateAnimation   畫面旋轉

ScaleAnimation     漸層尺寸縮放

TranslateAnimation 位置移動

AnimationSet          動畫集,讓多個動畫同時生效。

AnimationUtils        工具類,通過該類來載入xml實現的動畫。

 

(1) AlphaAnimation

    代碼方式:

Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  // 從0.1f變化到1.0fView.startAnimation(alphaAnimation );

     xml實現: alpha_anim.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <alpha          android:fromAlpha="0.1"          android:toAlpha="1.0"          android:duration="2000"     />  </set> 

 alphaAnimation= AnimationUtils.loadAnimation(getContext(), R.anim.alpha_anim);

 

(2) RotateAnimation

     代碼實現:

Animation rotateAnimation = new RotateAnimation(0f, 360f);  // 0是動畫起始時的角度,360f位動畫結束時的角度rotateAnimation.setDuration(1000); this.startAnimation(scaleAnimation);

     xml實現: rotate_anim.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <rotate          android:interpolator="@android:anim/accelerate_decelerate_interpolator"          android:fromDegrees="0"          android:toDegrees="360"          android:pivotX="50%"         <!--為動畫相對於物件的X、Y座標的開始位置, 50%表示物件的中點位置 -->        android:pivotY="50%"          android:duration="500"      />  </set> 

rotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_anim);

 

(3) ScaleAnimation

    代碼實現:

//初始化  Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);  // 前面兩個參數表示動畫起始、結束時X方向的伸縮尺寸,後面兩個參數表示動畫起始、結束時Y方向的伸縮尺寸。  //設定動畫時間  scaleAnimation.setDuration(500);  this.startAnimation(scaleAnimation);  

     xml實現: scanle_anim.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <scale          android:interpolator="@android:anim/accelerate_decelerate_interpolator"          android:fromXScale="0.0"          android:toXScale="1.0"          android:fromYScale="0.0"          android:toYScale="1.0"          android:pivotX="50%"          android:pivotY="50%"          android:fillAfter="false"          android:duration="500"      />     </set> 

scaleAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.scanle_anim);

 

(4) TranslateAnimation

    代碼實現:

Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);   // 四個參數(x1, x2, y1, y2),表示X方向從x1移動到x2, Y方向從y1移到y2. translateAnimation.setDuration(1000);     //設定動畫時間               this.startAnimation(translateAnimation);  

    xml實現: translate_anim.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <translate          android:fromXDelta="10"          android:toXDelta="100"          android:fromYDelta="10"          android:toYDelta="100"      />  </set> 

translateAnimation= AnimationUtils.loadAnimation(getContext(), R.anim.translate_anim);

 

(5) 使用AnimationSet, 讓多個動畫同時生效,如:

//初始化 Translate動畫  translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  //初始化 Alpha動畫  alphaAnimation = new AlphaAnimation(0.1f, 1.0f);    //動畫集  AnimationSet set = new AnimationSet(true);  set.addAnimation(translateAnimation);  set.addAnimation(alphaAnimation);    //設定動畫時間 (作用到每個動畫)  set.setDuration(1000);  this.startAnimation(set);  

 

2. Frame動畫(FrameAnimaiton)

    代碼實現過程:

      首先建立一個AnimationDrawable對象,通過addFrame方法把每一幀要顯示的內容添加進去,最後通過Start方法來播放動畫。

frameAnimation = new AnimationDrawable();  for (int i = 0; i < 10; i++) {      int id = getResources().getIdentifier("load" + (i+1), "drawable", this.getContext().getPackageName());       frameAnimation.addFrame(getResources().getDrawable(id), 100);  }  //設定迴圈播放   false表示迴圈  true表示不迴圈,僅播放一次  frameAnimation.setOneShot(false); //應用動畫image.setBackgroundDrawable(anim);anim.start(); 

     xml實現方式: frame_anim.xml

<?xml version="1.0" encoding="utf-8"?>  <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android"      android:oneshot="false">      <item android:drawable="@drawable/load1" android:duration="50" />      <item android:drawable="@drawable/load2" android:duration="50" />      <item android:drawable="@drawable/load3" android:duration="50" />      <item android:drawable="@drawable/load4" android:duration="50" />      <item android:drawable="@drawable/load5" android:duration="50" />      <item android:drawable="@drawable/load6" android:duration="50" />      <item android:drawable="@drawable/load7" android:duration="50" />      <item android:drawable="@drawable/load8" android:duration="50" />      <item android:drawable="@drawable/load9" android:duration="50" />      <item android:drawable="@drawable/load10" android:duration="50" />  </animaltion-list>  

應用frame_anim.xml:

image.setBackgroundResource(R.anim.frame);  //將動畫資源檔案設定為ImageView的背景  AnimationDrawable anim = (AnimationDrawable) image.getBackground(); //擷取ImageView背景,此時已被編譯成AnimationDrawable  anim.start();   //開始動畫 

 

3. 與動畫相關的一些函數及參數說明

 

表一

屬性[類型] 功能  
Duration[long] 屬性為動畫期間 時間以毫秒為單位
fillAfter [boolean] 當設定為true ,該動畫轉化在動畫結束後被應用
fillBefore[boolean] 當設定為true ,該動畫轉化在動畫開始前被應用

interpolator

指定一個動畫的插入器 有一些常見的插入器
accelerate_decelerate_interpolator
加速-減速 動畫插入器
accelerate_interpolator
加速-動畫插入器
decelerate_interpolator
減速- 動畫插入器
其他的屬於特定的動畫效果
repeatCount[int] 動畫的重複次數  
RepeatMode[int] 定義重複的行為 1:重新開始  2:plays backward
startOffset[long] 動畫之間的時間間隔,從上次動畫停多少時間開始執行下個動畫
zAdjustment[int] 定義動畫的Z Order的改變 0:保持Z Order不變
1:保持在最上層
-1:保持在最下層

Android基本動畫小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.