๋ชฉ์ฐจ
https://hanyeop.tistory.com/260
[Android] Jetpack Compose ์ ํธํฉ ์ปดํฌ์ฆ ์ฌ์ฉํด๋ณด๊ธฐ - (2) ๊ธฐ์ด ์ฌ์ฉ๋ฒ
2021.08.10 - [์๋๋ก์ด๋/Jetpack-Compose] - [Android] Jetpack Compose ์ ํธํฉ ์ปดํฌ์ฆ ์ฌ์ฉํด๋ณด๊ธฐ - (1) ๊ฐ๋ ๊ณผ ๊ตฌ์กฐ [Android] Jetpack Compose ์ ํธํฉ ์ปดํฌ์ฆ ์ฌ์ฉํด๋ณด๊ธฐ - (1) ๊ฐ๋ ๊ณผ ๊ตฌ์กฐ https://android-developers.googleblog
hanyeop.tistory.com
1. ์ฐ์ ์ฒซ ๋ฒ์งธ ๊ธฐ๋ณธ ํ๋ฉด์ ํด๋ฆญํ๋ฉด ๋ ๋ฒ์จฐ๋ก ๋์ด๊ฐ๋๋ก ํ๋ค.
2. ๊ธฐ๋ณธ ๋ก์ง ๊ตฌํํ๋ค.
3. ๊ธฐ๋ณธ ๋ก์ง ์ค ์ค๋ณต๋๋ ์ฝ๋ (Column ๋ถ๋ถ) ์ LemonTextAndImage๋ก ๋ฐ๊พผ๋ค. ->>>>>>>>>>๋ชจ๋ฅด๊ฒ ๋ค
@Composable
fun LemonTextAndImage(
textResource : Int,
drawableResource : Int,
//contentResource: Int,
onImageClick:() -> Unit,
modifier: Modifier = Modifier
){
Column (
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
){
Text(text = stringResource(textResource)
, fontSize = 18.sp)
Spacer(modifier = Modifier.height(16.dp))
Image(painter = painterResource(drawableResource),
contentDescription = "1",
modifier = Modifier
.wrapContentSize()
.clickable(onClick = onImageClick)
.border(
BorderStroke(2.dp, Color(105, 205, 216)),
shape = RoundedCornerShape(4.dp)
)
.padding(16.dp)
)
}//Column body
}
LemonTextAndImage() ํจ์์ ์ธ์๋ก textResource: Int, ํํ๋ก ์ฃผ๋๋ฐ ์ด ๋
R.string.select ๊ฐ์ ๊ฒ์ Int ํํ์ด๋ค.
๊ทผ๋ฐ onImageClick : () -> Unit ์ด ํํ ์ฐธ ์ดํด๊ฐ ์ ๊ฐ๋ค.
4. ์ฌ์ฉ์๊ฐ ๋ ๋ชฌ์ ์์ฐฉ(ํญ)ํด์ผ ํ๋ ํ์๋ 2์ 4 ์ฌ์ด์ ๋๋ค ์ซ์์ฌ์ผ ํ๋ค.
์ด ๋๋ค ์ซ์๋ ์ฌ์ฉ์๊ฐ ๋๋ฌด์์ ๋ ๋ชฌ์ ์๋ก ์ ํํ ๋๋ง๋ค ๋ฌ๋ผ์ง๋ค.
1 -> {
LemonTextAndImage(R.string.select,R.drawable.lemon_tree,
onImageClick = {
currentStep= 2
squeezeCount = (2..4).random()
})
} //1
step2์์๋ 2์ 4 ์ฌ์ด์ ๋๋ค ์ซ์๋ฅผ ์ฃผ์ด์ ํด๋น ์ ๋งํผ ํญ์ ํ ๋ค์ step3๋ก ๋์ด๊ฐ๋ค.
https://developer.android.com/jetpack/compose/modifiers-list#Border
ํ ๋๋ฆฌ ์ถ๊ฐ
Compose ์์ ์ ๋ชฉ๋ก | Jetpack Compose | Android Developers
Compose ์์ ์ ๋ชฉ๋ก ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. ์์ ๋ฒ์: ๋ชจ๋ ์ ๋ ฅ ๋๋ ์ ๊ทผ์ฑ 'ํด๋ฆญ' ์ด๋ฒคํธ๋ฅผ ํตํด ํด๋ฆญ์ด ๋ฐ์ํ๋๋ก ๊ตฌ์ฑ์์๋ฅผ
developer.android.com
modifier = Modifier
.wrapContentSize()
.clickable(onClick = onImageClick)
.border(
BorderStroke(2.dp, Color(105, 205, 216)),
shape = RoundedCornerShape(4.dp)
)
.padding(16.dp)
modifier.border() ๋ฅผ ํตํด ํ ๋๋ฆฌ๋ฅผ ์ค ์ ์๋ค.
์ ์ฒด
package com.example.lemonade
import android.graphics.Paint.Align
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.lemonade.ui.theme.LemonadeTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LemonadeTheme {
LemonApp()
}
}
}
}
@Composable
fun LemonTextAndImage(
textResource : Int,
drawableResource : Int,
//contentResource: Int,
onImageClick:() -> Unit,
modifier: Modifier = Modifier
){
Column (
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
){
Text(text = stringResource(textResource)
, fontSize = 18.sp)
Spacer(modifier = Modifier.height(16.dp))
Image(painter = painterResource(drawableResource),
contentDescription = "1",
modifier = Modifier
.wrapContentSize()
.clickable(onClick = onImageClick)
.border(
BorderStroke(2.dp, Color(105, 205, 216)),
shape = RoundedCornerShape(4.dp)
)
.padding(16.dp)
)
}//Column body
}
@Composable
fun LemonApp() {
var currentStep by remember {
mutableStateOf(1)
}
var squeezeCount by remember{
mutableStateOf(0)
}
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
//์กฐ๊ฑด๋ฌธ์ ๋ฐ๋ผ์ ์ด๋ฏธ์ง์ ํ์คํธ ๋ณด์ฌ์ฃฝ
when(currentStep) {
1 -> {
LemonTextAndImage(R.string.select,R.drawable.lemon_tree,
onImageClick = {
currentStep= 2
squeezeCount = (2..4).random()
})
} //1
2 -> {
LemonTextAndImage(R.string.select,R.drawable.lemon_squeeze,
onImageClick = {
squeezeCount -- // ํ๋ฒ ๋๋ฅผ ๋๋ง๋ค squeezeCount ํ๋์ฉ ๊ฐ์
if(squeezeCount==0){ // ๊ณ์ ํญ ํ๋ค๊ฐ 0์ด ๋๋ฉด 3๋จ๊ณ๋ก ๋์ด๊ฐ๋ค.
currentStep = 3
} //if
})
} //2
3->{
LemonTextAndImage(
textResource = R.string.drink,
drawableResource =R.drawable.lemon_drink ,
onImageClick = {
currentStep=4
})
} //3
4 -> {
LemonTextAndImage(
textResource = R.string.restart,
drawableResource = R.drawable.lemon_restart,
onImageClick = {
currentStep =1
})
}//4
} //when
} //Surface์ body
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
LemonadeTheme {
LemonApp()
}
}
์ฌ๊ธฐ์๋ onImageClick = { } ํํ ์ดํดํ๊ธฐ
'2023 ์๋๋ก์ด๋ > ์๋๋ก์ด๋ ์คํฐ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DiceRoller ๋ณต์ต (0) | 2023.01.10 |
---|---|
Unit2-1 Kotlin์์ null ํ์ฉ ์ฌ๋ถ ์ฌ์ฉ (0) | 2023.01.10 |
unit2- ๋ํํ Dice Roller ์ฑ ๋ง๋ค๊ธฐ (0) | 2022.12.29 |
taskmanager (0) | 2022.12.26 |
HappyBirthdayCard ์ฝ๋ (0) | 2022.12.24 |