カウントアプリをちょっと改造しよう
ここからは、ちょっとした小ネタを追加したり、見た目を変更していきましょう。
テキストとボタンの見た目を修正する
Section titled “テキストとボタンの見た目を修正する”
Text や Button の見た目を修正していきます。
@Composablefun CountAppScreen(modifier: Modifier = Modifier) {
var count by remember { mutableIntStateOf(0) }
Column( modifier = modifier ) { Text( text = "カウント: $count", fontSize = 40.sp, fontWeight = FontWeight.Bold, color = Color.Black, textAlign = TextAlign.Center )
Spacer(modifier = Modifier.height(24.dp))
Button( modifier = Modifier .size(120.dp) .scale(1f), onClick = { count++ } ) { Text( text = "+", fontSize = 48.sp, fontWeight = FontWeight.Bold, color = Color.White ) } }}
まずは前回までのコードを実行して、「カウント: 0」というテキストと「+」ボタンが表示されていることを確認しましょう。
もしも変化しなかった場合は、すぐに運営に伝えてね。
ボタンとテキストの位置を調整する
Section titled “ボタンとテキストの位置を調整する”
現状では、ボタンやテキストの位置が画面の左上に配置されています。これを画面の真ん中に配置するように調整していきましょう。
コードの追加
Section titled “コードの追加”
以下のコードを追加して、レイアウトを調整します。
@Composablefun CountAppScreen(modifier: Modifier = Modifier) {
var count by remember { mutableIntStateOf(0) }
Column( modifier = modifier .fillMaxSize() .background(Color.Cyan), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text( text = "カウント: $count",赤枠の中で、以下の設定がされています:
fillMaxSize()- サイズの最大化horizontalAlignment- 水平方向の調整verticalArrangement- 垂直方向の調整background()- 背景の色調整
コードを追加して実行すると、背景色がシアンになり、テキストとボタンが画面中央に配置されます。
数値によって背景色を変えてみよう
Section titled “数値によって背景色を変えてみよう”
次は、カウントの数値に応じて背景色が変わる機能を追加しましょう。10回タップするごとに背景色がランダムに変化します。
@Composablefun CountAppScreen(modifier: Modifier = Modifier) {
var count by remember { mutableIntStateOf(0) } val backgroundColor = remember { mutableStateOf(Color(0xFF1E1E1E)) }
LaunchedEffect(count) { if (count % 10 == 0 && count != 0) { backgroundColor.value = Color( Random.nextFloat(), Random.nextFloat(), Random.nextFloat(), 1f ) } }
Column( modifier = modifier .fillMaxSize() .background(backgroundColor.value), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text( text = "カウント: $count", fontSize = 40.sp, fontWeight = FontWeight.Bold, color = Color.White, textAlign = TextAlign.Center )背景が変わる動作の仕組み
Section titled “背景が変わる動作の仕組み”背景色の状態を保存
Section titled “背景色の状態を保存”以下のコードで、背景がどの色にしておくのか保存をしておきます。このときにmutableStateOfで変数を保存して、状態を管理できるようにします。
val backgroundColor = remember { mutableStateOf(Color(0xFF1E1E1E)) }背景の色が変わる仕組み
Section titled “背景の色が変わる仕組み”以下のコードは、countの値が変わったら呼び出されるものです。この時はLaunchedEffectを利用します。
LaunchedEffect(count) { if (count % 10 == 0 && count != 0) { backgroundColor.value = Color( Random.nextFloat(), Random.nextFloat(), Random.nextFloat(), 1f ) }}if文で10で割ったときにあまりが0だったら、背景の色を乱数で作成されるようになります。
Columnで背景色を使用
Section titled “Columnで背景色を使用”最後、背景の色の変数を、Columnで使用してあげれば、10クリックで1回背景色が変わるようになります。
Column( modifier = modifier .fillMaxSize() .background(backgroundColor.value), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,) { Text( text = "カウント: $count", fontSize = 40.sp, fontWeight = FontWeight.Bold, color = Color.White, textAlign = TextAlign.Center )
10タップに1回背景が変わるようになったと思います。
ボタンにアニメーションをつけてみよう
Section titled “ボタンにアニメーションをつけてみよう”次は、10回タップするごとにボタンの大きさが変わるアニメーションを追加します。
コードの追加
Section titled “コードの追加”
以下のコードを追加・編集してみましょう。
@Composablefun CountAppScreen(modifier: Modifier = Modifier) {
var count by remember { mutableIntStateOf(0) } val scale by animateFloatAsState( targetValue = if (count % 10 == 0 && count != 0) 1.5f else 1f, label = "scale" ) val backgroundColor = remember { mutableStateOf(Color(0xFF1E1E1E)) }
LaunchedEffect(count) { if (count % 10 == 0 && count != 0) {そして、Buttonのmodifierに.scale(scale)を追加します。
Button( modifier = Modifier .size(120.dp) .scale(scale), onClick = { count++ }) { Text( "+", fontSize = 48.sp, fontWeight = FontWeight.Bold, color = Color.White )}大きさが変わる理由
Section titled “大きさが変わる理由”animateFloatAsStateは、targetValueが変わったときに滑らかに値を変えてアニメーションをしてくれます。
val scale by animateFloatAsState( targetValue = if (count % 10 == 0 && count != 0) 1.5f else 1f, label = "scale")変更されたscaleはButtonのscaleと連動しています。
Button( modifier = Modifier .size(120.dp) .scale(scale), onClick = { count++ }
10回目、20回目でボタンの大きさが大きくなったり小さくなったり変更されるようになったと思います。
これで、基本となるアプリは完成になります
Section titled “これで、基本となるアプリは完成になります”
お疲れ様でした。
今回のレポジトリはこちらからみれます: https://github.com/harutiro/CountAppSysHack ↗