๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2023 ์•ˆ๋“œ๋กœ์ด๋“œ/์•ˆ๋“œ๋กœ์ด๋“œ ์Šคํ„ฐ๋””

unit2- ๋Œ€ํ™”ํ˜• Dice Roller ์•ฑ ๋งŒ๋“ค๊ธฐ

by hyeonha 2022. 12. 29.

๋ชฉ์ฐจ

    3. ๋ ˆ์ด์•„์›ƒ ์ธํ”„๋ผ ๋งŒ๋“ค๊ธฐ ?????

    ์ˆ˜์ •์ž ์ถ”๊ฐ€ 

    @Preview(showBackground = true)
    @Composable
    fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
    
    }

    ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด modifier ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. modifier ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ ๊ฐ’์€ Modifier ๊ฐ์ฒด์ด๋ฏ€๋กœ 

    ๋ฉ”์„œ๋“œ ์„œ๋ช…์˜ =Modifier ๋ถ€๋ถ„์ด๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ์ดํ›„์— ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ „๋‹ฌํ• ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ž์ฒด Modifier ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๋ฉด UI ๋™์ž‘๊ณผ ์žฅ์‹์„ ๋งž์ถค ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    Modifier ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์ธ  ์ผ๋‹จ Modifier ๊ฐ์ฒด๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š”๋ฐ ๊ตณ์ด Modifier ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•˜๋Š”์ง€ ์˜๋ฌธ์ด ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ปดํฌ์ €๋ธ”์ด ์žฌ๊ตฌ์„ฑ์„ ๊ฑฐ์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์žฌ๊ตฌ์„ฑ: ๊ธฐ๋ณธ์ ์œผ๋กœ @Composavle ๋ฉ”์„œ๋“œ์˜ ์ฝ”๋“œ ๋ธ”๋Ÿญ์ด ๋‹ค์‹œ ์‹คํ–‰๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.  Modifier ๊ฐ์ฒด๊ฐ€ ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ๋งŒ๋“ค์–ด์ง€๋ฉด ๋‹ค์‹œ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด๋Š” ํšจ์œจ์ ์ด์ง€ ์•Š๋‹ค.

    ๋ญ” ๋ง์ธ์ง€ ๋ชจ๋ฅด๊ฒ ์‚ผ

    @Composable
    fun DiceRollerApp(){
        DiceWithButtonAndImage(modifier =Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.Center))
    
    }

    4 ๋ฒ„ํŠผ ์ถ”๊ฐ€

    DiceWithButtonAndImage(modifier: Modifier = Modifier){
        Column (
            modifier=modifier,
            horizontalAlignment = Alignment.CenterHorizontally
        ){
            Button(onClick = { /*TODO*/ }) {
    
                Text(stringResource(R.string.roll))
            }
        }
    }
    

    6. ์ด๋ฏธ์ง€ ์ถ”๊ฐ€

    @Preview(showBackground = true)
    @Composable
    fun DiceWithButtonAndImage(modifier: Modifier = Modifier){
        Column (
            modifier=modifier,
            horizontalAlignment = Alignment.CenterHorizontally
        )
        {
            Image(
                painter = painterResource(R.drawable.dice_1),
            contentDescription = "1"
            )
            Spacer(modifier = Modifier.height(16.dp))
            Button(onClick = { /*TODO*/ }) {
    
                Text(stringResource(R.string.roll))
            }
    
        }
    }
    

    7. ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ ๋กœ์ง ๋นŒ๋“œ 

    Button์„ ๋Œ€ํ™”ํ˜•์œผ๋กœ ๋งŒ๋“ค๊ธฐ 

     

     

     9. ๊ฒฐ๋ก 

    ์š”์•ฝ

    Compose๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Android์šฉ ๋Œ€ํ™”ํ˜• Dice Roller ์•ฑ์„ ๋งŒ๋“ค์—ˆ๋‹ค.

    -๊ตฌ์„ฑ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค.

    - ์ปดํฌ์ง€์…˜์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ ๋‹ค -> ?

    - Button ์ปดํฌ์ €๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒ„ํŠผ์„ ๋งŒ๋“ ๋‹ค.

    - Image ์ปดํฌ์ €๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

     - ์ปดํฌ์ €๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€ํ™”ํ˜• UI๋ฅผ ๋งŒ๋“ ๋‹ค.

    - remember ์ปดํฌ์ €๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ์ง€์…˜์˜ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•œ๋‹ค.

    - mutableStageOf() ํ•จ์ˆ˜๋กœ UI๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ observable์„ ๋งŒ๋“ ๋‹ค. 

    728x90