Android幀動畫實現一

Android幀動畫就是把一組連續的圖片按照一定的順序給它播放出來動畫效果:

一、首先先給出一組連續的圖片,這裏就不給了,大家自己找,然後把圖片放在項目對應的drawable文件下或者mipmap文件夾下看你自己。

二、然後在drawable文件下新建一個xml文件,這裏命名爲granule_list_anim.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="40" android:drawable="@mipmap/rectangle_025"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_026"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_027"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_028"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_029"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_030"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_031"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_032"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_033"/>
    <item android:duration="40" android:drawable="@mipmap/rectangle_034"/>
</animation-list>

這裏簡單說一下,幀動畫是一組圖片,所以外層使用  animation-list  標籤,

每個item 中的 duration  爲每一幀圖片的持續時間,這裏給40 單位爲毫秒,具體時間根據你的需求以及效果定,後面的drawable 裏面就是你的圖片了,記得按順序寫,

還有一個重要的屬性; android:oneshot="false"  這個是在 animation-list 標籤裏面的,注意了,他有兩個值,一個true 還有一個false  ,true 代表不循環播放,false代表幀動畫結束之後,繼續重頭開始循環播放,我開始看別的寫的這麼循環播放的時候,很多人介紹都不一樣,有的說true是循環,有的說是false是循環把我搞糊塗了,這裏親自實踐,false是循環,,是否循環僅限本篇幀動畫寫法。一會還有另一種實現方法,比這種要節省內存。但是我沒搞出來怎麼循環,這裏暫且先不說。

三、開始寫代碼:

定義一個 AnimationDrawable  對象,然後使用ImageView獲取他的Drawable

AnimationDrawable animationDrawable;


ImageView boxImg;//記得先findViewById,或者BindView都行,


// granule_list_anim 就是上面定義的drawable xml文件
boxImg.setImageResource(R.drawable.granule_list_anim);

animationDrawable = (AnimationDrawable) boxImg.getDrawable();

//接着調用start即可實現幀動畫的循環播放
animationDrawable.start();

//animationDrawable 還有其他的方法,根據需要自己添加

好了,這個循環播放的或者不循環播放就實現了。下一篇給個比這個更省內存的幀動畫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章