Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

android - How to Generate from random image imageView to be unrandom / squentially kotlin

How i generate squentially from the random imageView. i need make generator from button roll but the result is squentially dice number in format image? this for 2D dice, please help and thank you very much sorry may bad english

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

}

question from:https://stackoverflow.com/questions/65840015/how-to-generate-from-random-image-imageview-to-be-unrandom-squentially-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...