exception
exception

Reputation: 35

get the current animation frame by AnimationDrawable on android

I have two images for animation. each of them have three images. the first animation has start/stop button. the second animation played automatically. when the 3rd frame of 1st animation and 3rd frame of 2nd animation is played on the same time, I want to stop all animation. how can I check if the 3rd frame is being played for both images on the same time?

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>

Upvotes: 0

Views: 294

Answers (1)

pranav malayil
pranav malayil

Reputation: 86

there is no listeners for AnimationDrawble class. so this is not possible but you can you do it creating a custom class by extending AnimationDrawble

Upvotes: 0

Related Questions