android動畫-Frame Animation

來源:互聯網
上載者:User

標籤:frame   animation   android   

轉載請註明出處: http://blog.csdn.net/forwardyzk/article/details/42739281

Drawable Animation(Frame Animation):幀動畫,連續播放和播放gif圖片的效果是一樣的。

可以使用xml構建Drawable Animation,也可以使用代碼編寫Drawable Animation動畫

下面先介紹使用xml構建Drawable Animation動畫

在res目錄下建立anim目錄

建立xml檔案

例如:

drwable_fr.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item        android:drawable="@drawable/bird1"        android:duration="300"/>    <item        android:drawable="@drawable/bird2"        android:duration="300"/>    <item        android:drawable="@drawable/bird3"        android:duration="300"/>    <item        android:drawable="@drawable/bird4"        android:duration="300"/>    <item        android:drawable="@drawable/bird5"        android:duration="300"/>    <item        android:drawable="@drawable/bird6"        android:duration="300"/>    <item        android:drawable="@drawable/bird7"        android:duration="300"/>    <item        android:drawable="@drawable/bird8"        android:duration="300"/></animation-list>



必須以animation-list為根節點,item為子節點,

android:oneshot="false":連續播放,true:表示只播放一次

android:drawable="@drawable/bird1" 制定播放的幀圖片

android:duration="300" 幀播放的時間


在代碼中指定View的播放動畫

image = (ImageView) findViewById(R.id.image);image.setBackgroundResource(R.anim.drwable_fr);drwableAnimation = (AnimationDrawable) image.getBackground();drwableAnimation.start();drwableAnimation.stop();

先指定幀動畫資源為View的背景,擷取背景,轉換為AnimationDrawable,

start()開始播放,stop為停止播放


下面介紹使用代碼構建Drwable Animation

anim = new AnimationDrawable();for (int i = 1; i <= 3; i++) {// 根據資源名稱和目錄擷取R.java中對應的資源IDint id = getResources().getIdentifier("jump" + i, "drawable",getPackageName());// 根據資源ID擷取到Drawable對象Drawable drawable = getResources().getDrawable(id);// 將此幀添加到AnimationDrawable中anim.addFrame(drawable, 300);}anim.setOneShot(false); // 設定為loop

int id = getResources().getIdentifier("jump" + i, "drawable",getPackageName());

第一個參數為:圖片的名字,第二個參數:圖片所在的目錄,第三個參數:應用的包名



形成Drawable對象:Drawable drawable = getResources().getDrawable(id);

添加到動畫中:anim.addFrame(drawable, 300);

anim.setOneShot(false);設定是否迴圈播放,false:是,true,只播放一次


注意:
不要在onCreate中調用start,因為AnimationDrawable還沒有完全跟Window相關聯,如果想要介面顯示時就開始動畫的話,可以在onWindowFoucsChanged()中調用start()。

原始碼下載: http://download.csdn.net/detail/forwardyzk/8363995

:






android動畫-Frame Animation

聯繫我們

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