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

Android ์•ฑ์— ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ ~5

by hyeonha 2022. 11. 16.

๋ชฉ์ฐจ

    Image composable ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์— ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

    https://github.com/google-developer-training/basic-android-kotlin-compose-birthday-card-app/blob/main/app/src/main/res/drawable-nodpi/androidparty.png

     

    GitHub - google-developer-training/basic-android-kotlin-compose-birthday-card-app

    Contribute to google-developer-training/basic-android-kotlin-compose-birthday-card-app development by creating an account on GitHub.

    github.com

    ์ด ๋งํฌ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.

    C: hyeon ์— ์ €์žฅํ•˜๊ธฐ

    Resource Manager์—์„œ + ํ•œ ํ›„ Import Drawables ํด๋ฆญ

    Qualifier Type๋ชฉ๋ก์—์„œ Density,

       Value๋ชฉ๋ก์—์„œ no density ํด๋ฆญ

    Import ํด๋ฆญ

       -> Android ์ŠคํŠœ๋””์˜ค๋Š” drawable-noopi ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ์น˜ํ•œ๋‹ค. 

    ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋Š” Drawable ํƒญ ์•„๋ž˜์˜ ๋ชฉ๋ก์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

     

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

    ์•ฑ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ํ‘œ์‹œํ•  ์œ„์น˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 

    Text composable์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ Image composable์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œ ๊ฐ€๋Šฅ

     

    Jetpack Compose์˜ ๋ฆฌ์†Œ์Šค

    ๋ฆฌ์†Œ์Šค๋Š” ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€ ํŒŒ์ผ๊ณผ ์ •์ ์ธ ์ฝ˜ํ…์ธ ์ด๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด ๋น„ํŠธ๋งต, ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ๋ฌธ์ž์—ด, ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ง€์นจ ๋“ฑ์ด ์žˆ๋‹ค.

    ์ด๋ฏธ์ง€์™€ ๋ฌธ์ž์—ด ๋“ฑ application resource๋Š” ๋…๋ฆฝ์ ์ธ ์œ ์ง€๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ํ•ญ์ƒ ์ฝ”๋“œ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•ด์•ผํ•œ๋‹ค.

    Android๋Š” ๋Ÿฐํƒ€์ž„์— ํ˜„์žฌ ๊ตฌ์„ฑ์„ ๊ทผ๊ฑฐ๋กœ ์ ์ ˆํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

    ex) ํ™”๋ฉด ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ UI ๋ ˆ์ด์•„์›ƒ์„ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ์–ธ์–ด ์„ค์ •์— ๋”ฐ๋ผ ๋ฌธ์ž์—ด์„ ์ œ๊ณตํ•œ๋‹ค. 

     

    ๋ฆฌ์†Œ์Šค ๊ทธ๋ฃนํ™”

    ํ•ญ์ƒ ํ”„๋กœ์ ํŠธ์˜ res/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ํŠน์ • ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๊ฐ ์œ ํ˜•์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐฐ์น˜ํ•ด์•ผ ํ•œ๋‹ค. 

    ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ๊ณ„์ธต ๊ตฌ์กฐ์˜ ์˜ˆ

    MyProject/
        src/
            MyActivity.kt
        res/
            drawable/
                graphic.png
            mipmap/
                icon.png
            values/
                strings.xml

    res/ directory์—๋Š” ์ด๋ฏธ์ง€ ๋ฆฌ์†Œ์Šค์šฉ drawable ๋””๋ ‰ํ„ฐ๋ฆฌ,

    ๋Ÿฐ์ฒ˜ ์•„์ด์ฝ˜์šฉ mipmap/ ๋””๋ ‰ํ„ฐ๋ฆฌ,

    ๋ฌธ์ž์—ด ๋ฆฌ์†Œ์Šค์šฉ values/ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํฌํ•จ๋œ ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. 

     

    ๋ฆฌ์†Œ์Šค ์—‘์„ธ์Šค

    Jetpack Compose๋Š” Android ํ”„๋กœ์ ํŠธ์— ์ •์˜๋œ ๋ฆฌ์†Œ์Šค์— ์—‘์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค. 

    ํ”„๋กœ์ ํŠธ์˜ R ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค ID๋กœ ๋ฆฌ์†Œ์Šค์— access ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    R ํด๋ž˜์Šค๋Š” Android์—์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํด๋ž˜์Šค๋กœ, ํ”„๋กœ์ ํŠธ์— ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์†Œ์Šค์˜ ID๋ฅผ ํฌํ•จํ•œ๋‹ค. 

    ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฆฌ์†Œ์Šค ID๋Š” ํŒŒ์ผ ์ด๋ฆ„๊ณผ ๋™์ผํ•˜๋‹ค. 

    ์˜ˆ๋ฅผ ๋“ค์–ด ์ด์ „ ํŒŒ์ผ ๊ณ„์ธต ๊ตฌ์กฐ์˜ ์ด๋ฏธ์ง€๋Š” 

    R.drawable.graphic

    ๋กœ ์—‘์„ธ์Šค ๊ฐ€๋Šฅ

    ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด๋ณด์ž

    1. BirthdayGreetingWithImage() ํ•จ์ˆ˜์—์„œ val ์†์„ฑ์„ ์„ ์–ธํ•˜๊ณ  ์ด๋ฆ„์„ image๋กœ ์ง€์ •
    2. androidparty ๋ฆฌ์†Œ์Šค๋ฅผ ์ „๋‹ฌํ•˜์—ฌ painterResource() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ image์— ํ• ๋‹น
    //Image composable
    @Composable
    fun BirthdayGreetingWithImage(message: String,from: String) {
        val image = painterResource(R.drawable.androidparty)
        
    }

    painterResource() ํ•จ์ˆ˜๋Š”

    drawable ์ด๋ฏธ์ง€ ๋ฆฌ์†Œ์Šค๋ฅผ ๋กœ๋“œํ•˜๊ณ  ๋ฆฌ์†Œ์Šค ID(์—ฌ๊ธฐ์„œ๋Š” R.drawable.androidparty) ๋ฅผ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค. 

    3. painterResource() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ํ›„ Image ์ปดํฌ์ €๋ธ”์„ ์ถ”๊ฐ€ํ•˜๊ณ  image๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ painter ์ธ์ˆ˜๋กœ ์ „๋‹ฌ

    Image(
       painter = image
    )

     

    ์•ฑ ์ ‘๊ทผ์„ฑ ํ™•์ธ

    ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œ๋˜๋Š” ์ฝ˜ํ…์ธ  ์„ค๋ช…์„ ์„ค์ •ํ•˜๋Š” ๋Œ€์‹  ์ด๋ฏธ์ง€์˜ contentDescription ์ธ์ˆ˜๋ฅผ null๋กœ ์„ค์ •ํ•˜์—ฌ

    TalkBack์ด Image ์ปดํฌ์ €๋ธ”์„ ๊ฑด๋„ˆ๋›ฐ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

    cf) Talkback: ์‚ฌ์šฉ์ž์—๊ฒŒ ์ œ๊ณตํ•˜๋Š” ์Œ์„ฑํ”ผ๋“œ๋ฐฑ, ์‚ฌ์šฉ์ž๋Š” ํ™”๋ฉด์„ ๋ณด์ง€ ์•Š๊ณ ๋„ ๊ธฐ๊ธฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

     

    Image(
       painter = image,
       contentDescription = null
    )

    Image ์ปดํฌ์ €๋ธ” ๋ฏธ๋ฆฌ๋ณด๊ธฐ 

     

    ๋‚œ ์™œ ์ด๋ ‡๊ฒŒ ๋œฐ๊นŒ 

    5. Box ๋ ˆ์ด์•„์›ƒ ์ถ”๊ฐ€

    Compose์˜ ์„ธ ๊ฐ€์ง€ ๊ธฐ๋ณธ ํ‘œ์ค€ ๋ ˆ์ด์•„์›ƒ ์š”์†Œ๋Š” Column, Row, Box ์ปดํฌ์ €๋ธ”์ด๋‹ค.

    ์ด์ œ Box ์ปดํฌ์ €๋ธ”์— ๊ด€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค~

     

    Box ๋ ˆ์ด์•„์›ƒ์€ Compose์˜ ํ‘œ์ค€ ๋ ˆ์ด์•„์›ƒ ์š”์†Œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 

    Box ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์„œ๋กœ ์œ„์— ์Œ“๋Š”๋‹ค. Box๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ํฌํ•จ๋œ ์š”์†Œ์˜ ํŠน์ € ์ •๋ ฌ๋„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

    1. Image ์ปดํฌ์ €๋ธ” ์ฃผ์œ„์— Box ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€

    @Composable
    fun BirthdayGreetingWithImage(message: String,from: String) {
        val image = painterResource(R.drawable.androidparty)
        //Box ๋ ˆ์ด์•„์›ƒ ์ถ”๊ฐ€
        Box {
            
            Image(painter = image, contentDescription = null)
        }
    
    }

    2. Box ์ปดํฌ์ €๋ธ”์˜ ๋์—์„œ BirthdayGreetingWithText() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ›„  ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์ผ ๋ฉ”์‹œ์ง€์™€ ์„œ๋ช…์„ ์ „๋‹ฌ

     

    3. ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํ™•์ธ 

    4. ์—๋ฎฌ๋ ˆ์ดํ„ฐ๋‚˜ ๊ธฐ๊ธฐ์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๋ฐ˜์˜๋˜๋„๋ก ํ•˜๋ ค๋ฉด onCreate() ํ•จ์ˆ˜์—์„œ BirthdayGreetingWithText()ํ•จ์ˆ˜ ํ˜ธ์ถœ์„  BirthdayGreetingWithImage() ํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                HappyBirthdayTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(color = MaterialTheme.colors.background){
                       BirthdayGreetingWithImage(message = "Happy Birthday Sam!", from = "-from Emma")
                }
                }
            }
        }
    }

     

     

    728x90