詳解Android動畫之Frame Animation(轉)

來源:互聯網
上載者:User

標籤:

在開始執行個體講解之前,先引用官方文檔中的一段話:

Frame動畫是一系列圖片按照一定的順序展示的過程,和放電影的機制很相似,我們稱為逐幀動畫。Frame動畫可以被定義在XML檔案中,也可以完全編碼實現。

如果被定義在XML檔案中,我們可以放置在/res下的anim或drawable目錄中(/res/[anim | drawable]/filename.xml),檔案名稱可以作為資源ID在代碼中引用;如果由完全由編碼實現,我們需要使用到AnimationDrawable對象。

如果是將動畫定義在XML檔案中的話,文法如下:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot=["true" | "false"] >    <item        android:drawable="@[package:]drawable/drawable_resource_name"        android:duration="integer" /></animation-list>

需要注意的是:

<animation-list>元素是必須的,並且必須要作為根項目,可以包含一或多個<item>元素;android:onshot如果定義為true的話,此動畫只會執行一次,如果為false則一直迴圈。

<item>元素代表一幀動畫,android:drawable指定此幀動畫所對應的圖片資源,android:druation代表此幀持續的時間,整數,單位為毫秒。

文檔接下來的樣本我就不在解說了,因為接下來我們也要結合自己的執行個體示範一下這個過程。

我們建立一個名為anim的工程,將四張連續的圖片分別命名為f1.png,f2.png,f3.png,f4.png,放於drawable目錄,然後建立一個frame.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/f1" android:duration="300" />    <item android:drawable="@drawable/f2" android:duration="300" />    <item android:drawable="@drawable/f3" android:duration="300" />    <item android:drawable="@drawable/f4" android:duration="300" /></animation-list>

我們可以將frame.xml檔案放置於drawable或anim目錄,官方文檔上是放到了drawable中了,大家可以根據喜好來放置,放在這兩個目錄都是可以啟動並執行。

然後介紹一下布局檔案res/layout/frame.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <ImageView      android:id="@+id/frame_image"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:layout_weight="1"/>  <Button      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="stopFrame"      android:onClick="stopFrame"/>  <Button      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="runFrame"      android:onClick="runFrame"/></LinearLayout>

我們定義了一個ImageView作為動畫的載體,然後定義了兩個按鈕,分別是停止和啟動動畫。

接下來介紹一下如何通過載入動畫定義檔案來實現動畫的效果。我們首先會這樣寫:

package com.scott.anim;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class FrameActivity extends Activity {        private ImageView image;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.frame);        image = (ImageView) findViewById(R.id.frame_image);                image.setBackgroundResource(R.anim.frame);        AnimationDrawable anim = (AnimationDrawable) image.getBackground();        anim.start();    }}

看似十分完美,跟官方文檔上寫的一樣,然而當我們運行這個程式時會發現,它只停留在第一幀,並沒有出現我們期望的動畫,也許你會失望的說一句:“Why?”,然後你把相應的代碼放在一個按鈕的點擊事件中,動畫就順利執行了,再移回到onCreate中,還是沒效果,這個時候估計你會氣急敗壞的吼一句:“What the fuck!”。但是,什麼原因呢?如何解決呢?

出現這種現象是因為當我們在onCreate中調用AnimationDrawable的start方法時,視窗Window對象還沒有完全初始化,AnimationDrawable不能完全追加到視窗Window對象中,那麼該怎麼辦呢?我們需要把這段代碼放在onWindowFocusChanged方法中,當Activity展示給使用者時,onWindowFocusChanged方法就會被調用,我們正是在這個時候實現我們的動畫效果。當然,onWindowFocusChanged是在onCreate之後被調用的,

然後我們需要重寫一下代碼:

package com.scott.anim;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class FrameActivity extends Activity {        private ImageView image;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.frame);        image = (ImageView) findViewById(R.id.frame_image);    }        @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);        image.setBackgroundResource(R.anim.frame);        AnimationDrawable anim = (AnimationDrawable) image.getBackground();        anim.start();    }}

運行一下,動畫就可以正常顯示了。

如果在有些場合,我們需要用純程式碼方式實現一個動畫,我們可以這樣寫:

AnimationDrawable anim = new AnimationDrawable();for (int i = 1; i <= 4; i++) {    int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());    Drawable drawable = getResources().getDrawable(id);    anim.addFrame(drawable, 300);}anim.setOneShot(false);image.setBackgroundDrawable(anim);anim.start();

完整的FrameActivity.java代碼如下:

package com.scott.anim;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class FrameActivity extends Activity {        private ImageView image;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.frame);        image = (ImageView) findViewById(R.id.frame_image);    }        @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);        image.setBackgroundResource(R.anim.frame);    //將動畫資源檔案設定為ImageView的背景        AnimationDrawable anim = (AnimationDrawable) image.getBackground();    //擷取ImageView背景,此時已被編譯成AnimationDrawable        anim.start();    //開始動畫    }        public void stopFrame(View view) {        AnimationDrawable anim = (AnimationDrawable) image.getBackground();        if (anim.isRunning()) {    //如果正在運行,就停止            anim.stop();        }    }        public void runFrame(View view) {        //完全編碼實現的動畫效果        AnimationDrawable anim = new AnimationDrawable();        for (int i = 1; i <= 4; i++) {            //根據資源名稱和目錄擷取R.java中對應的資源ID            int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());            //根據資源ID擷取到Drawable對象            Drawable drawable = getResources().getDrawable(id);            //將此幀添加到AnimationDrawable中            anim.addFrame(drawable, 300);        }        anim.setOneShot(false);    //設定為loop        image.setBackgroundDrawable(anim);    //將動畫設定為ImageView背景        anim.start();    //開始動畫    }}

轉載地址:http://blog.csdn.net/liuhe688/article/details/6657776

詳解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.