逐帧动画(Frame Animation)

   逐帧动画也叫Drawable Animation,是最简单最直观的动画类型,它利用人眼的视觉暂留效应(也就是光对视网膜所产生的视觉),在光停止作用后,仍然会保留一段事件的现象。
    在Android中实现逐帧动画,就是由设计师给出一系列状态不断变化的图片,开发者可以指定动画中每一帧对应的图片和持续事件,然后就可以开始播放动画,具体有两种方式可以定义逐帧动画,分别是采用XML资源文件和代码实现。

1. XML资源文件方式

   这是最常用的方式,首先我们将每一帧的图片放到res/drawable目录中,然后在res/anim目录中新建一个动画XML文件,在这个文件中使用<animation-list>标签来定义动画帧序列,使用<item>标签来定义动画的每一帧,并在其中指定帧的持续时间等属性:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shuibo0000" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0001" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0002" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0003" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0004" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0005" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0006" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0007" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0008" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0009" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0010" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0011" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0012" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0013" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0014" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0015" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0016" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0017" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0018" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0019" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0020" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0021" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0022" android:duration="120"/>
    <item android:drawable="@drawable/shuibo0023" android:duration="120"/>
</animation-list>
 <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/shuibowen_animtaion" />
val animationDrawable = imageView.drawable as AnimationDrawable
        if(!animationDrawable.isRunning){
            animationDrawable.start()
        }

2.代码方式

   在代码中定义逐帧动画也很简单,但不常用:

   val animationDrawable = AnimationDrawable()
        for (i in 0 until 24) {
            val identifier = resources.getIdentifier(
                if (i - 10 < 0) {
                    "shuibo000$i"
                } else "shuibo00$i", "drawable", packageName
            )
            val drawable = resources.getDrawable(identifier)
            animationDrawable.addFrame(drawable, 120)
        }
        imageView.setImageDrawable(animationDrawable)
        animationDrawable.isOneShot = false
        animationDrawable.start()

逐帧动画

猜你喜欢

转载自blog.csdn.net/weixin_44785058/article/details/107993234