Android 逐幀動畫創(chuàng)建實例詳解
Android 逐幀動畫創(chuàng)建實例詳解
前言:
我們看早期電影的時候,電影通常是一張一張播放,用我們現(xiàn)在專有名詞來說,就是一幀幀來,安卓同樣有這樣動畫效果的編排形式。
那么我們先定義逐幀動畫xml文件
<?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/pic1" android:duration="200" /> <item android:drawable="@drawable/pic2" android:duration="200" /> <item android:drawable="@drawable/pic3" android:duration="200" /> <item android:drawable="@drawable/pic4" android:duration="200" /> <item android:drawable="@drawable/pic5" android:duration="200" /> <item android:drawable="@drawable/pic6" android:duration="200" /> <item android:drawable="@drawable/pic7" android:duration="200" /> <item android:drawable="@drawable/pic8" android:duration="200" /> <item android:drawable="@drawable/pic8" android:duration="200" /> </animation-list>
main.xml
<ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="98dp" android:layout_marginTop="69dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="54dp" android:layout_marginLeft="98dp" android:onClick="startMovie" android:text="開始播放電影" />
Activiy代碼:
public class MyAnimationDemo extends Activity { private AnimationDrawable draw=null; private ImageView image; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_animation_demo); image=(ImageView)super.findViewById(R.id.pic); } public void startMovie(View v){ image.setBackgroundResource(R.anim.oldvideo);//第一步,設(shè)置圖片資源 draw=(AnimationDrawable)image.getBackground();//取得圖片背景的Drawable draw.setOneShot(false);//動畫執(zhí)行次數(shù) draw.start();//開始動畫 } }
這里我們看到,
第一步,設(shè)置圖片背景資源
第二步,設(shè)置得到圖片背景的draw
第三步,設(shè)置draw參數(shù),并start()
實現(xiàn)效果如下,間隔0.2秒即換圖,實現(xiàn)老電影動畫效果
以上就是Android 逐幀動畫的實例詳解,如有疑問請留言或到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android ViewFlipper翻轉(zhuǎn)視圖使用詳解
這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper翻轉(zhuǎn)視圖的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android之軟鍵盤自動彈出和關(guān)閉【代碼分享】
本文主要介紹了Android中軟鍵盤自動彈出和關(guān)閉的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android游戲開發(fā)實踐之人物移動地圖的平滑滾動處理
玩過rpg游戲的朋友應(yīng)該都知道RPG的游戲地圖一般都比較大 今天我和大家分享一下在RPG游戲中如何來處理超出手機屏幕大小的游戲地圖。2014-06-06Android實現(xiàn)退出時關(guān)閉所有Activity的方法
這篇文章主要介紹了Android實現(xiàn)退出時關(guān)閉所有Activity的方法,主要通過自定義類CloseActivityClass實現(xiàn)這一功能,需要的朋友可以參考下2014-09-09