幀動畫animation-list

幀動畫是順序包房一組預先定義好的圖片,使用比較簡單。如下

xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_sunny"
        android:duration="500" />
    <item
        android:drawable="@drawable/ic_night"
        android:duration="500" />
    <item
        android:drawable="@drawable/ic_cloudy"
        android:duration="500" />
</animation-list>

佈局

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_animation_list" />

代碼實現

    iv_image.setImageResource(R.drawable.bg_animation_list)
        var d: AnimationDrawable =
            iv_image.drawable as AnimationDrawable
        d.start()
        iv_image.setOnClickListener {
            if (d.isRunning) {
                d.stop()
            } else {
                d.start()
            }
        }

效果

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