You need to enable JavaScript to run this app. Kotlin开发Android骰子应用:使用Thread.sleep时ImageView仅更新最后一帧
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin开发Android骰子应用:使用Thread.sleep时ImageView仅更新最后一帧

问题根因

Android的所有界面更新操作都运行在主线程(UI线程),主线程靠内部的消息队列依次处理UI更新请求。你直接在主线程调用Thread.sleep()会阻塞整个主线程,所有setImageResource触发的重绘请求都会被挂起,直到你整个点击事件的代码全部执行完毕才会统一处理,所以你看不到中间的过渡帧,只会显示最终的图片。

可行解决方案

这里给两种常用实现方案,都不会阻塞主线程:

方案1:Kotlin协程(推荐,Kotlin Android开发首选异步方案)

新版AndroidX已经自带生命周期协程域lifecycleScope,绑定Activity生命周期,不会出现内存泄漏。

代码修改步骤:

  1. 删掉原rollDice方法里的Thread.sleep和过渡动画逻辑,只保留最终结果更新的逻辑
  2. 修改点击事件和动画逻辑:
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val rollButton: Button = findViewById(R.id.button)
        val diceImage: ImageView = findViewById(R.id.imageView)
        val dice = Dice(6)

        rollButton.setOnClickListener {
            rollButton.isClickable = false
            // 启动协程,执行异步动画逻辑
            lifecycleScope.launch {
                // 播放滚动动画:随机切换骰子面5次,每次间隔200ms,参数可以自行调整
                val allDiceDrawables = listOf(
                    R.drawable.dice_1, R.drawable.dice_2, R.drawable.dice_3,
                    R.drawable.dice_4, R.drawable.dice_5, R.drawable.dice_6
                )
                repeat(5) {
                    diceImage.setImageResource(allDiceDrawables.random())
                    delay(200) // 协程延迟,不会阻塞主线程
                }
                // 设置最终随机结果
                val finalRoll = dice.roll()
                val finalDrawable = when (finalRoll) {
                    1 -> R.drawable.dice_1
                    2 -> R.drawable.dice_2
                    3 -> R.drawable.dice_3
                    4 -> R.drawable.dice_4
                    5 -> R.drawable.dice_5
                    else -> R.drawable.dice_6
                }
                diceImage.setImageResource(finalDrawable)
                diceImage.contentDescription = finalRoll.toString()
                rollButton.isClickable = true
            }
        }
        // 启动时默认掷一次骰子
        rollDice()
    }

    // 原rollDice方法可以保留,用于初始加载
    private fun rollDice() {
        val dice = Dice(6)
        val diceRoll = dice.roll()
        val diceImage: ImageView = findViewById(R.id.imageView)
        val drawableResource = when (diceRoll) {
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
        diceImage.setImageResource(drawableResource)
        diceImage.contentDescription = diceRoll.toString()
    }
}

class Dice(private val numSides: Int) {
    fun roll(): Int {
        return (1..numSides).random()
    }
}

方案2:View.postDelayed(原生API,无需额外依赖)

如果不想用协程,可以用Android原生的延迟执行API,原理是把每个帧的更新任务放到主线程的消息队列,到时间自动执行:

rollButton.setOnClickListener {
    rollButton.isClickable = false
    val diceImage: ImageView = findViewById(R.id.imageView)
    // 第一帧
    diceImage.setImageResource(R.drawable.dice_1)
    // 1000ms后更新第二帧
    diceImage.postDelayed({
        diceImage.setImageResource(R.drawable.dice_2)
        // 再1000ms后更新最终结果
        diceImage.postDelayed({
            rollDice()
            rollButton.isClickable = true
        }, 1000)
    }, 1000)
}

该方案适合帧数少的简单动画,帧数多的话会出现多层回调嵌套,可读性不如协程方案。

内容的提问来源于stack exchange,提问作者DKay36

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 00:54:05