android之動畫通過AnimationDrawable控制逐幀動畫

來源:互聯網
上載者:User

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:
//AnimotionDrawable布局檔案

<!-- 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()


android與逐幀動畫:

:

當我們點擊按鈕時,該圖片會不停的旋轉,當再次點擊按鈕時,會停止在當前的狀態。

activity代碼:

    package cn.com.chenzheng_java.animation;            import android.app.Activity;      import android.graphics.drawable.AnimationDrawable;      import android.os.Bundle;      import android.view.View;      import android.widget.ImageView;      /**      * @description android中的逐幀動畫.      * 逐幀動畫的原理很簡單,跟電影的播放一樣,一張張類似的圖片不停的切換,當切換速度達到一定值時,      * 我們的視覺就會出現殘影,殘影的出現保證了視覺上變化的連續性,這時候圖片的切換看在我們眼中就跟真實的一樣了。      * 想使用逐幀動畫:      * 第一步:需要在res/drawable檔案夾下建立一個xml檔案,該檔案詳細定義了動畫播放時所用的圖片、切換每張圖片      *        所用的時間、是否為連續播放等等。(有些文章說,在res/anim檔案夾下放置該檔案,事實證明,會出錯哦)      * 第二步:在代碼中,將該動畫布局檔案,賦值給特定的圖片展示控制項,如本例子中的ImageView。      * 第三步:通過imageView.getBackGround()擷取相應的AnimationDrawable對象,然後通過該對象的方法進行控制動畫      * @author chenzheng_java      *      */      public class Animation1Activity extends Activity {          ImageView imageView ;          @Override          public void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.animation1);                            imageView = (ImageView) findViewById(R.id.imageView_animation1);              imageView.setBackgroundResource(R.drawable.animation1_drawable);                        }                              public void myClickHandler(View targetButton){              // 擷取AnimationDrawable對象              AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();                            // 動畫是否正在運行              if(animationDrawable.isRunning()){                  //停止動畫播放                  animationDrawable.stop();              }              else{                  //開始或者繼續動畫播放                  animationDrawable.start();              }                                      }      }  

animation1.xml檔案:

    <?xml version="1.0" encoding="utf-8"?>      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_width="match_parent" android:layout_height="match_parent"          android:orientation="vertical">          <Button  android:id="@+id/button_animation1" android:text="動畫開始"              android:layout_gravity="center_horizontal" android:layout_width="wrap_content"              android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>          <ImageView android:id="@+id/imageView_animation1"              android:layout_width="fill_parent"               android:layout_height="wrap_content" android:layout_weight="1"></ImageView>      </LinearLayout>  

存放動畫檔案的xml檔案:

    <?xml version="1.0" encoding="utf-8"?>      <!--           根標籤為animation-list,其中oneshot代表著是否只展示一遍,設定為false會不停的迴圈播放動畫          根標籤下,通過item標籤對動畫中的每一個圖片進行聲明          android:duration 表示展示所用的該圖片的時間長度       -->      <animation-list        xmlns:android="http://schemas.android.com/apk/res/android"        android:oneshot="false"        >          <item android:drawable="@drawable/a1" android:duration="50"></item>          <item android:drawable="@drawable/a2" android:duration="50"></item>          <item android:drawable="@drawable/a3" android:duration="50"></item>          <item android:drawable="@drawable/a4" android:duration="50"></item>          <item android:drawable="@drawable/a5" android:duration="50"></item>          <item android:drawable="@drawable/a6" android:duration="50"></item>      </animation-list>  

除此之外:在AnimationDrawable中,我們還可以看到如下的幾個重要方法:

setOneShot(boolean flag) 和在設定檔中進行配置一樣,可以設定動畫是否播放一次,false為連續播放;

addFrame
(Drawable frame, int duration) 動態添加一個圖片進入該動畫中



聯繫我們

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