在Android

时间:2019-06-24 04:25:31

标签: android animationdrawable

我有两幅动画图像。每个都有三个图像。第一个动画具有开始/停止按钮。第二个动画自动播放。当同时播放第一动画的第三帧和第二动画的第三帧时,我想停止所有动画。如何检查是否同时播放两个图像的第三帧?

GameActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game)

        val sonImage = findViewById<ImageView>(R.id.sonid).apply {
            setBackgroundResource(R.drawable.son)
            sonAnimation = background as AnimationDrawable
        }

        val fatherImage = findViewById<ImageView>(R.id.fatherid).apply {
            setBackgroundResource(R.drawable.father)
            fatherAnimation = background as AnimationDrawable

        }

        startPlay.setOnClickListener {
            sonAnimation.start()
        }

        stop.setOnClickListener {
            sonAnimation.stop()
        }

        val handler = Handler()
        handler.postDelayed(object : Runnable {
            override fun run() {
                randomValues = Random.nextLong(0, 10)
                fatherAnimation.start()
                handler.postDelayed(this, (randomValues*1000)+941)

            }
        }, 2000)

        val handler1 = Handler()
        handler1.postDelayed(object : Runnable {
            override fun run() {
                fatherAnimation.stop()
                handler1.postDelayed(this, (randomValues*1000)+940)

            }
        }, 2940)

    }

son.xml

<?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                    android:oneshot="false">
        <item android:drawable="@drawable/son1" android:duration="235" />
        <item android:drawable="@drawable/son2" android:duration="235" />
        <item android:drawable="@drawable/son3" android:duration="235" />
    </animation-list>

father.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false" >
    <item android:drawable="@drawable/father1" android:duration="235" />
    <item android:drawable="@drawable/father2" android:duration="235" />
    <item android:drawable="@drawable/father3" android:duration="235" />
</animation-list>

1 个答案:

答案 0 :(得分:0)

没有用于AnimationDrawble类的侦听器。 因此这是不可能的,但是您可以通过扩展AnimationDrawble

来创建自定义类