如何解决如何使用 Kotlin 在 androidStudio 中更改手机方向时保存我的图像
如何在更改我的 android 应用程序的方向时保存图像这基本上是一个简单的掷骰子应用程序,即基于随机数,在此基础上我们可以在 kotlin 中使用 when() 轻松选择图像 包 com.example.rollingdice
import android.location.Address
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import javax.crypto.KeyGenerator
import kotlin.properties.Delegates
import kotlin.random.Random
abstract class MainActivity : AppCompatActivity() {
lateinit var showingResult:ImageView
lateinit var diceButton:Button
lateinit var result
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
diceButton=findViewById(R.id.rollDice)
showingResult=findViewById(R.id.iv)
diceButton.setOnClickListener {
rollingDice()
}
}
// accordin to me this function is use to save the data or image while changing the
orientation
override fun onSaveInstanceState(outState: Bundle,outPersistentState: PersistableBundle) {
super.onSaveInstanceState(outState,outPersistentState)
outState.putInt("img",result)
}
//private function named as rollingDice()
private fun rollingDice() {
var number : Int= Random.nextInt(6)+1
// taking random number input in when()
when (number){
// taking number in when() statement to choose the image
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
// else condition give the dice_6 name image as a result
}
showingResult.setImageResource(result)
//i'm taking the result as aimage in the image view
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。