我如何从随机图像视图中按顺序生成。 我需要从按钮滚动生成器,但结果是格式图像中的骰子数量?这对于 2D 骰子, 请帮助并非常感谢 对不起,可能英语不好
package com.example.dice
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
private lateinit var imgDice: ImageView
private val diceImages: MutableList<Int> = mutableListOf()
private lateinit var animation: Animation
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imgDice = findViewById(R.id.imgDice)
val btnRoll = findViewById<Button>(R.id.btnRoll)
btnRoll.setOnClickListener {
getRandomValue()
}
diceImages.add(R.drawable.dice1)
diceImages.add(R.drawable.dice2)
diceImages.add(R.drawable.dice3)
diceImages.add(R.drawable.dice4)
diceImages.add(R.drawable.dice5)
diceImages.add(R.drawable.dice6)
animation = AnimationUtils.loadAnimation(this@MainActivity, R.anim.shake_anim)
}
private fun getRandomValue() {
val random = Random().nextInt(6)
imgDice.setImageResource(diceImages.elementAt(random))
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
imgDice.startAnimation(animation)
}
override fun onAnimationEnd(animation: Animation?) { imgDice.setImageResource(diceImages.elementAt(random))
}
override fun onAnimationStart(animation: Animation?) {
imgDice.setImageResource(R.drawable.dice1)
}
})
}
}