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([email protected], 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

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


Jawaban

Belum ada Jawaban, jadi yang pertama

Login untuk gabung berdiskusi