Kotlin的Android应用程序:更新imageView仅更新最后一个选项



我正在尝试"动画化";在谷歌Kotlin学习路径的骰子滚动应用程序中滚动骰子。我尝试用可绘制的";睡眠;一点点,然后再次更新,这几次,直到它停止在随机生成的数字。

当我使用(选项1(:

rollButton.setOnClickListener {
rollButton.isClickable = false
rollDice()
Thread.sleep(1000)
rollDice()
Thread.sleep(1000)
rollDice()
rollButton.isClickable = true
}

它只是睡觉,不更新3次。图像仅在2秒钟后更新一次。

当我尝试在rollDice()内部使用(选项2(:处理它时也是如此

// Play Roll - Animation
diceImage.setImageResource(R.drawable.dice_1)
Thread.sleep(1000)
diceImage.setImageResource(R.drawable.dice_2)
Thread.sleep(1000)
// Update the ImageView with the correct drawable resource ID
diceImage.setImageResource(drawableResource)

应用程序:(https://developer.android.com/codelabs/basic-android-kotlin-training-dice-roller-images?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics kotlin four%23codelab https%3A%2F%2developer.android.com%2Fcodelabs%2basics android kotlin training dice roller images#6(

代码:

class MainActivity : AppCompatActivity() {
/**
* This method is called when the Activity is created.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Find the Button in the layout
val rollButton: Button = findViewById(R.id.button)
// Set a click listener on the button to roll the dice when the user taps the button
rollButton.setOnClickListener {
// HERE WOULD BE OPTION 1  -------------------!!!!!!
rollButton.isClickable = false
rollDice()
rollButton.isClickable = true
}
// Do a dice roll when the app starts
rollDice()
}
/**
* Roll the dice and update the screen with the result.
*/
private fun rollDice() {
// Create new Dice object with 6 sides and roll it
val dice = Dice(6)
val diceRoll = dice.roll()
// Find the ImageView in the layout
val diceImage: ImageView = findViewById(R.id.imageView)
// Determine which drawable resource ID to use based on the dice roll
val drawableResource = when (diceRoll) {
1 -> R.drawable.dice_1
2 -> R.drawable.dice_2
3 -> R.drawable.dice_3
4 -> R.drawable.dice_4
5 -> R.drawable.dice_5
else -> R.drawable.dice_6
}
// Play Roll - Animation
// HERE IS OPTION 2 -------------------!!!!!!
diceImage.setImageResource(R.drawable.dice_1)
Thread.sleep(1000)
diceImage.setImageResource(R.drawable.dice_2)
Thread.sleep(1000)
// Update the ImageView with the correct drawable resource ID
diceImage.setImageResource(drawableResource)
// Update the content description
diceImage.contentDescription = diceRoll.toString()
}
}
/**
* Dice with a fixed number of sides.
*/
class Dice(private val numSides: Int) {
/**
* Do a random dice roll and return the result.
*/
fun roll(): Int {
return (1..numSides).random()
}
}

我试了三天才找到答案,有什么想法吗?

我认为这是因为您正在设置图像资源,然后立即让负责重新绘制视图和更新图像视图的主线程休眠一秒钟,然后再做同样的事情。这是不可取的,但如果你想做这样的动画,试着使用Coroutine。

但是如果你仍然想使用线程,你可以使用这个在点击监听器中写下这个

new Thread() {
@Override
public void run() {
try {
rollDiceInMain();
Thread.sleep(1000);
rollDiceInMain();
Thread.sleep(1000);
rollDiceInMain();
runOnUiThread {
rollButton.isClickable = true
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();

还有另一种方法rollDiceOn就像这个

private void rollDiceInMain() {
runOnUiThread {
rollDice()
}
}

相关内容

  • 没有找到相关文章

最新更新