Android Frame 動畫

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   使用   sp   for   

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

 

下面就講一下Frame Animation。

 

其實使用起來比較簡單,首先需要建立一個AnimationDrawable對象,通過addFrame方法把每一幀要顯示的內容添加進去,最後通過Start方法來播放動畫。 同時還有設定迴圈setOneShot等方法可供使用。

 

 

下面先看一下官方對AnimationDrawable類的說明:

 

An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object‘s background.

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call run() to start the animation.

An AnimationDrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame of the animation. See the example below.

spin_animation.xml file in res/drawable/ folder:

<!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder --> <animation-list android:id="selected" android:oneshot="false">    <item android:drawable="@drawable/wheel0" android:duration="50" />    <item android:drawable="@drawable/wheel1" android:duration="50" />    <item android:drawable="@drawable/wheel2" android:duration="50" />    <item android:drawable="@drawable/wheel3" android:duration="50" />    <item android:drawable="@drawable/wheel4" android:duration="50" />    <item android:drawable="@drawable/wheel5" android:duration="50" /> </animation-list>

Here is the code to load and play this animation.

 // Load the ImageView that will host the animation and // set its background to our AnimationDrawable XML resource. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setBackgroundResource(R.drawable.spin_animation); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start()
 
相信看了這個說明,大家也都知道如何做了吧? 
下面我們使用Frame動畫做一個 loading 的效果。
首先準備素材,如下:

然後建立一個AnimationDrawable對象,把這些圖片載入進去。 addFrame第一參數表示要載入的內容,第二參數表示期間。
代碼:
[java] view plaincopyprint?
  1. frameAnimation = new AnimationDrawable();  
  2. for (int i = 0; i < 10; i++) {  
  3.             int id = getResources().getIdentifier("load" + (i+1), "drawable", this.getContext().getPackageName());  
  4.             frameAnimation.addFrame(getResources().getDrawable(id), 100);  
  5.         }  
  6.           
  7.         //設定迴圈播放   false表示迴圈  true表示不迴圈,僅播放一次  
  8.         frameAnimation.setOneShot(false);  
AnimationDrawable 類是Drawable類的子類,因此可以將 AnimationDrawable  設為背景等來查看效果。
最後只要調用  frameAnimation.start();  就可以啟動該Frame動畫了。
改日補上。
那麼上面是使用Java代碼的方式來載入的Frame動畫, 也可以像Tween動畫那樣,使用純XML局部檔案來做。 API中已經有說明,這邊不再贅述了。
直接看樣本:
[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:oneshot="false">  
  4.     <item android:drawable="@drawable/load1" android:duration="50" />  
  5.     <item android:drawable="@drawable/load2" android:duration="50" />  
  6.     <item android:drawable="@drawable/load3" android:duration="50" />  
  7.     <item android:drawable="@drawable/load4" android:duration="50" />  
  8.     <item android:drawable="@drawable/load5" android:duration="50" />  
  9.     <item android:drawable="@drawable/load6" android:duration="50" />  
  10.     <item android:drawable="@drawable/load7" android:duration="50" />  
  11.     <item android:drawable="@drawable/load8" android:duration="50" />  
  12.     <item android:drawable="@drawable/load9" android:duration="50" />  
  13.     <item android:drawable="@drawable/load10" android:duration="50" />  
  14. </animaltion-list>  
有不清楚之處,歡迎留言交流。
附上參數詳細說明(來自: http://www.moandroid.com/?p=790)

表一

屬性[類型] 功能  
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 Frame 動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.