Bagaimana generate dari imageView tanpa random

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)
            }

        })

    }
}
avatar pusakaofjavasheor_ray
@pusakaofjavasheor_ray

1 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

Tanggapan

jangan hanya copypaste kode kamu ya. Jelaskan apa yang dibuat, apa masalahnya dan di bagian mana, edit pertanyaan kamu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban