Android中AnimationDrawable使用的簡單一實例_Android

來源:互聯網
上載者:User

首先,可以在drawable檔案夾下定義一個xml的檔案。如下所示:

複製代碼 代碼如下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/compass_1" android:duration="70" />
    <item android:drawable="@drawable/compass_2" android:duration="70" />
    <item android:drawable="@drawable/compass_3" android:duration="70" />
    <item android:drawable="@drawable/compass_4" android:duration="70" />
    <item android:drawable="@drawable/compass_5" android:duration="70" />
</animation-list>

根標籤為animation-list,其中oneshot代表著是否只展示一遍,設定為false會不停的迴圈播放動畫。其中,每一個item是一幀,android:duration="400"表示每幀持續400ms,android:drawable是每幀要顯示的圖片。
接下來,代碼中使用:

複製代碼 代碼如下:

AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable(R.drawable.bootanimation);

得到AnimationDrawable對象。再將該對象設為當前view的backgroud。

複製代碼 代碼如下:

mView.setBackgroundDrawable(ad);

接著,就可以調用 ad.start()方法和ad.stop()方法來啟動和停止該動畫。
注意:必須設為當前view的background,否則start和stop該動畫是沒有效果的。

聯繫我們

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