歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中如何創建Frame Animation?

Android中如何創建Frame Animation?

日期:2017/3/1 11:08:09   编辑:Linux編程

Frame Animation是在一系列的圖片之間產生切換的動畫效果。在xml文件描述不同的圖片,文件放在res/anim目錄下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:Android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@drawable/icon1" android:duration="1000"></item>
<item android:drawable="@drawable/icon2" android:duration="1000"></item>
<item android:drawable="@drawable/icon3" android:duration="1000"></item>
</animation-list>

android:oneshot用於指定動畫是播放一次還是循環播放。

創建一個java類文件:

ImageButton image=(ImageButton)findViewById(R.id.image_button);
image.setBackgroundResource(R.anim.frame_anim);
AnimationDrawable anim=(AnimationDrawable)image.getBackground();
anim.start();

值得注意的是,Frame Animation不能在Activity中的onCreate()方法啟動,因為此時對應的動畫資源沒有准備好,可以在onWindowFocusChanged()方法內使用。

Copyright © Linux教程網 All Rights Reserved