帧动画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()
            }
        }

效果

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