簡體   English   中英

在android上通過AnimationDrawable獲取當前動畫幀

[英]get the current animation frame by AnimationDrawable on android

我有兩個動畫圖像。 他們每個人都有三個圖像。 第一個動畫有開始/停止按鈕。 第二個動畫自動播放。 當第一動畫的第三幀和第二動畫的第三幀同時播放時,我想停止所有動畫。 如何檢查是否同時為兩個圖像播放第三幀?

游戲活動.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)

    }

子文件

<?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>

父親.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>

AnimationDrawble 類沒有偵聽器。 所以這是不可能的,但你可以通過擴展 AnimationDrawble 來創建自定義類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM