๊นจ์•Œ ๊ฐœ๋… ๐Ÿ“‘/Android

[Android] Jetpack Compose - Surface

interfacer_han 2024. 7. 22. 01:32

#1 ๊ฐœ์š”

 

androidx.compose.material3  |  Android Developers

androidx.compose.desktop.ui.tooling.preview

developer.android.com

Surface๋Š” Composable๋“ค์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ, Composable์˜ ์ง‘ํ•ฉ์„ ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋„์™€์ค€๋‹ค.

 

#2 ๊ตฌ์กฐ

@Composable
@ComposableInferredTarget
public fun Surface(
    modifier: Modifier, // Surface์— ์ ์šฉ๋  Modifier
    shape: Shape, // Surface์˜ ๋ชจ์–‘ ์ •์˜
    color: Color, // ๋ฐฐ๊ฒฝ์ƒ‰
    contentColor: Color, // Surface ์† Composable๋“ค์—๊ฒŒ ์ ์šฉ๋  ๊ธฐ๋ณธ ์ƒ‰ ์ผ๊ด„ ์ง€์ •.
    tonalElevation: Dp, // ์ƒ‰์ƒ ํ†ค(tone)์— ์˜ํ•œ ์‹œ๊ฐ์  ๊ณ„์ธตํ™”(Elevation)
    shadowElevation: Dp, // ๊ทธ๋ฆผ์ž์˜ ํฌ๊ธฐ์— ์˜ํ•œ ์‹œ๊ฐ์  ๊ณ„์ธตํ™”(Elevation)
    border: BorderStroke?, // ์šฐ๋ฆฌ๊ฐ€ ์ž˜ ์•„๋Š” ๊ทธ border
    content: @Composable () -> Unit
): Unit

Surface์˜ ๊ตฌ์กฐ๋‹ค. ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

modifier
Modifier๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

shape

RoundedCornerShape, CircleShape ๋“ฑ์œผ๋กœ Surface์˜ ๋ชจ์–‘์„ ์ง€์ •ํ•œ๋‹ค.

 

color
๋ฐฐ๊ฒฝ์ƒ‰์ด๋‹ค.

 

contentColor

Surface ์†์— ์žˆ๋Š” content๋“ค์— ์ƒ‰์„ ๋ช…์‹œํ•˜์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ๋  ์ƒ‰์ด๋‹ค.

 

tonalElevation

Surface ๋ผ๋ฆฌ ์ค‘์ฒฉ๋œ ๊ฒฝ์šฐ ์ƒ‰(tone)์„ ํ†ตํ•ด ์ค‘์ฒฉ๋œ Surface ๊ณ„์ธต์„ ์‹œ๊ฐํ™”ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์กฐ๊ฑด์ด ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ, ๊ฐ€์žฅ ์ƒ์œ„์— ์กด์žฌํ•˜๋Š” Surface์˜ color ์†์„ฑ์ด ColorScheme.surface๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. #3 ๋ฐ #4๋ฅผ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค.

 

shadowElevation

Surface์— ๊ทธ๋ฆผ์ž ํšจ๊ณผ๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.

 

border

ํ…Œ๋‘๋ฆฌ๋ฅผ ์ •์˜ํ•œ๋‹ค.

 

content

Surface ๋‚ด๋ถ€์˜ ์ž์‹๋“ค์ด๋‹ค. ๋žŒ๋‹ค ํ‘œํ˜„์‹(์ด ๊ธ€์˜ #2-5 ์ฐธ์กฐ) ํ˜•ํƒœ๋กœ, Surface( ... ) { ... }์˜ ๊ตฌ์กฐ์—์„œ ์ค‘๊ด„ํ˜ธ ๋ถ€๋ถ„์ด ๋ฐ”๋กœ ์ด content๋‹ค.

 

Surface์˜ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋ถ€ ์ด์šฉํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ์•„๋ž˜์—์„œ ๋งŒ๋“ค์–ด๋ณธ๋‹ค.

 

#3 3๊ฐœ์˜ Surface๋ฅผ ์ค‘์ฒฉํ•œ ์ฝ”๋“œ

// package com.example.surface

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.LocalAbsoluteTonalElevation
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.surface.ui.theme.SurfaceTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SurfaceTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background, // ColorScheme์€ '์ƒ‰์ƒ ๋ชจ์Œ'์ด๋‹ค.
                    tonalElevation = 0.dp, // <- ์ด๊ฒŒ ๊ธฐ๋ณธ๊ฐ’์ด๋ผ ์ƒ๋žตํ•ด๋„ ๋จ
                    border = BorderStroke(8.dp, Color.Red)
                ) {
                    Box(
                        modifier = Modifier.fillMaxSize(), // <- ์ด๊ฒŒ ๊ธฐ๋ณธ๊ฐ’์ด๋ผ ์ƒ๋žตํ•ด๋„ ๋จ
                        contentAlignment = Alignment.Center
                    ) {
                        Surface(
                            modifier = Modifier.fillMaxSize(0.8f),
                            shape = RoundedCornerShape(36.dp),
                            tonalElevation = LocalAbsoluteTonalElevation.current + 16.dp,
                            shadowElevation = 12.dp,
                        ) {
                            Box(
                                modifier = Modifier.fillMaxSize(), // <- ์ด๊ฒŒ ๊ธฐ๋ณธ๊ฐ’์ด๋ผ ์ƒ๋žตํ•ด๋„ ๋จ
                                contentAlignment = Alignment.Center
                            ) {
                                Surface(
                                    modifier = Modifier.fillMaxSize(0.8f),
                                    tonalElevation = LocalAbsoluteTonalElevation.current + 16.dp,
                                    contentColor = Color.Blue
                                ) {
                                    Greeting("Android")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        fontSize = 24.sp,
        modifier = modifier,
    )
}

3๊ฐœ์˜ Surface๋ฅผ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ์ค‘์ฒฉ์‹œ์ผฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  #2์˜ ๊ฐ์ข… ์†์„ฑ๋“ค์„ ์ ์šฉ์‹œ์ผฐ๋‹ค.

 

#4 ์ž‘๋™ ํ™•์ธ

tonalElevation์€ ๋ผ์ดํŠธ๋ชจ๋“œ์—์„œ๋Š” ์ ์  ์–ด๋‘์›Œ์ง€๊ณ , ๋‹คํฌ๋ชจ๋“œ์—์„œ๋Š” ์ ์  ๋ฐ์•„์ง„๋‹ค.

 

#5 ์š”์•ฝ

Surface๋Š” Composable๋“ค์˜ Container๋‹ค.

 

#6 ์™„์„ฑ๋œ ์•ฑ

 

android-practice/jetpack-compose/Surface at master ยท Kanmanemone/android-practice

Contribute to Kanmanemone/android-practice development by creating an account on GitHub.

github.com