如何在一定时间后自动更改图像?

huangapple go评论88阅读模式
英文:

How do I make the image change automatically after a certain time?

问题

以下是您要翻译的代码部分:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {

        R.id.nav_sair -> {
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
            finish()
            Toast.makeText(this, "Logue novamente", Toast.LENGTH_LONG).show()
            true
        }

        R.id.nav_sobre -> {
            binding.imvTroca.setImageResource(R.drawable.perfectaprint_quemsomos_full_editado)
            true
        }

        R.id.nav_impressoras -> {
            binding.imvTroca.setImageResource(R.drawable.imagem_impressoras_edit)
            true
        }

        R.id.nav_produtos_materiais -> {
            binding.imvTroca.setImageResource(R.drawable.produtos_materiais)
            true
        }

        R.id.nav_fornecedores -> {
            binding.imvTroca.setImageResource(R.drawable.fornecedores)
            true
        }

        else -> {
            super.onOptionsItemSelected(item)
        }

    }
}

With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.

英文:

With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.

 override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {

        R.id.nav_sair -> {
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
            finish()
            Toast.makeText(this, "Logue novamente", Toast.LENGTH_LONG).show()
            true
        }

        R.id.nav_sobre -> {
            binding.imvTroca.setImageResource(R.drawable.perfectaprint_quemsomos_full_editado)
            true
        }

        R.id.nav_impressoras -> {
            binding.imvTroca.setImageResource(R.drawable.imagem_impressoras_edit)
            true
        }

        R.id.nav_produtos_materiais -> {
            binding.imvTroca.setImageResource(R.drawable.produtos_materiais)
            true
        }

        R.id.nav_fornecedores -> {
            binding.imvTroca.setImageResource(R.drawable.fornecedores)
            true
        }

        else -> {
            super.onOptionsItemSelected(item)

        }

    }

}

}

With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.enter code here

答案1

得分: 0

你可以使用协程,如果它被中断,你可以替换它。

private val imvTrocaImageChangeJob: Job? = null

private fun setTemporaryImvTrocaImage(imageResId: Int) {
    imvTrocaImageChangeJob?.cancel() // 中断任何现有的图像更改任务
    imvTrocaImageChangeJob = lifecycleScope.launch {
       binding.imvTroca.setImageResource(imageResId)
        delay(2000L) // 2000 毫秒
       binding.imvTroca.setImageResource(R.drawable.the_original_image)
    }
}

// ...

R.id.nav_produtos_materiais -> {
setTemporaryImvTrocaImage(R.drawable.produtos_materiais)
true
}

// 等等...

英文:

You can use a coroutine that you can replace if it gets interuppted.

private val imvTrocaImageChangeJob: Job? = null

private fun setTemporaryImvTrocaImage(imageResId: Int) {
    imvTrocaImageChangeJob?.cancel() // interrupt any existing image change job
    imvTrocaImageChangeJob = lifecycleScope.launch {
       binding.imvTroca.setImageResource(imageResId)
        delay(2000L) // 2000 milliseconds
       binding.imvTroca.setImageResource(R.drawable.the_original_image)
    }
}
    // ...

    R.id.nav_produtos_materiais -> {
        setTemporaryImvTrocaImage(R.drawable.produtos_materiais)
        true
    }

    // etc. ...

huangapple
  • 本文由 发表于 2023年7月4日 22:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定