Advertisement
backlight0815

Untitled

Dec 1st, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2022 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.dice
  17.  
  18. import android.os.Bundle
  19. import androidx.activity.ComponentActivity
  20. import androidx.activity.compose.setContent
  21. import androidx.compose.foundation.Image
  22. import androidx.compose.foundation.layout.Column
  23. import androidx.compose.foundation.layout.fillMaxSize
  24. import androidx.compose.foundation.layout.height
  25. import androidx.compose.foundation.layout.Spacer
  26. import androidx.compose.foundation.layout.wrapContentSize
  27. import androidx.compose.material.Button
  28. import androidx.compose.material.Text
  29. import androidx.compose.runtime.Composable
  30. import androidx.compose.runtime.getValue
  31. import androidx.compose.runtime.mutableStateOf
  32. import androidx.compose.runtime.remember
  33. import androidx.compose.runtime.setValue
  34. import androidx.compose.ui.Alignment
  35. import androidx.compose.ui.Modifier
  36. import androidx.compose.ui.res.painterResource
  37. import androidx.compose.ui.res.stringResource
  38. import androidx.compose.ui.tooling.preview.Preview
  39. import androidx.compose.ui.unit.dp
  40. import androidx.compose.ui.unit.sp
  41. import com.example.dice.R
  42. import com.example.dice.ui.theme.DiceTheme
  43. import com.example.dice.ui.theme.DiceTheme
  44.  
  45. class MainActivity : ComponentActivity() {
  46. override fun onCreate(savedInstanceState: Bundle?) {
  47. super.onCreate(savedInstanceState)
  48. setContent {
  49. DiceTheme() {
  50. DiceRollerApp()
  51. }
  52. }
  53. }
  54. }
  55.  
  56. @Preview
  57. @Composable
  58. fun DiceRollerApp() {
  59. DiceWithButtonAndImage(modifier = Modifier
  60. .fillMaxSize()
  61. .wrapContentSize(Alignment.Center)
  62. )
  63. }
  64.  
  65. @Composable
  66. fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
  67. var result by remember { mutableStateOf( 1) }
  68. val imageResource = when(result) {
  69. 1 -> R.drawable.dice_1
  70. 2 -> R.drawable.dice_2
  71. 3 -> R.drawable.dice_3
  72. 4 -> R.drawable.dice_4
  73. 5 -> R.drawable.dice_5
  74. else -> R.drawable.dice_6
  75. }
  76. Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
  77. Image(painter = painterResource(imageResource), contentDescription = result.toString())
  78. Spacer(modifier = Modifier.height(16.dp))
  79. Button(onClick = { result = (1..6).random() }) {
  80. Text(text = stringResource(R.string.roll), fontSize = 24.sp)
  81. }
  82. }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement