화면에 표시되는 내용을 정확하게 제어하며 정교한 작업이 필요로 할때 사용
View는 수백만개 의 픽셀이 하나로 모여 화면을 생성하게됩니다.
Canvas(modifier = Modifier.fillMaxSize()) {
val canvasWidth = size.width
val canvasHeight = size.height
drawLine(
start = Offset(x = canvasWidth, y = 0f),
end = Offset(x = 0f, y = canvasHeight),
color = Color.Blue
)
}
Canvas(modifier = Modifier.fillMaxSize()) {
val canvasWidth = size.width
val canvasHeight = size.height
drawCircle(
color = Color.Blue,
center = Offset(x = canvasWidth / 2, y = canvasHeight / 2),
radius = size.minDimension / 4
)
}
Canvas(modifier = Modifier.fillMaxSize()) {
val canvasSize = size
val canvasWidth = size.width
val canvasHeight = size.height
drawRect(
color = Color.Gray,
topLeft = Offset(x = canvasWidth / 3F, y = canvasHeight / 3F),
size = canvasSize / 3F
)
}
둥근 직사각형 + 원 + 그라데이션 으로 이루어 져있다.
그리고 그레디언트가 필요하며 둥근 사각형안에 둥근 도넛원을 획 채우기로 만들어야 합니다. 코드로 한 번 보겠습니다.
val instaColors = listOf(Color.Yellow, Color.Red, Color.Magenta)
Canvas(
modifier = Modifier
.size(100.dp)
.padding(16.dp)
) {
drawRoundRect(
brush = Brush.linearGradient(colors = instaColors),
cornerRadius = CornerRadius(60f, 60f),
style = Stroke(width = 15f, cap = StrokeCap.Round)
)
drawCircle(
brush = Brush.linearGradient(colors = instaColors),
radius = 45f,
style = Stroke(width = 15f, cap = StrokeCap.Round)
)
drawCircle(
brush = Brush.linearGradient(colors = instaColors),
radius = 13f,
center = Offset(this.size.width * .80f, this.size.height * 0.20f),
)
}
둥근 사각형 + 텍스트 사용자 글꼴 + 색상
val assetManager = LocalContext.current.assets
val paint = Paint().apply {
textAlign = Paint.Align.CENTER
textSize = 200f
color = Color.White.toArgb()
typeface = Typeface.createFromAsset(assetManager, "FACEBOLF.OTF")
}
Canvas(
modifier = Modifier
.size(100.dp)
.padding(16.dp)
) {
drawRoundRect(
color = Color(0xFF1776d1),
cornerRadius = CornerRadius(20f, 20f)
)
drawContext.canvas.nativeCanvas.drawText("f", center.x + 25, center.y + 90, paint)
}
타원형 + 그리디언트 + Path
val colors = listOf(Color(0xFF02b8f9), Color(0xFF0277fe))
Canvas(
modifier = Modifier
.size(100.dp)
.padding(16.dp)
) {
val trianglePath = Path().let {
it.moveTo(this.size.width * .20f, this.size.height * .77f)
it.lineTo(this.size.width * .20f, this.size.height * 0.95f)
it.lineTo(this.size.width * .37f, this.size.height * 0.86f)
it.close()
it
}
val electricPath = Path().let {
it.moveTo(this.size.width * .20f, this.size.height * 0.60f)
it.lineTo(this.size.width * .45f, this.size.height * 0.35f)
it.lineTo(this.size.width * 0.56f, this.size.height * 0.46f)
it.lineTo(this.size.width * 0.78f, this.size.height * 0.35f)
it.lineTo(this.size.width * 0.54f, this.size.height * 0.60f)
it.lineTo(this.size.width * 0.43f, this.size.height * 0.45f)
it.close()
it
}
drawOval(
Brush.verticalGradient(colors = colors),
size = Size(this.size.width, this.size.height * 0.95f)
)
drawPath(
path = trianglePath,
Brush.verticalGradient(colors = colors),
style = Stroke(width = 15f, cap = StrokeCap.Round)
)
drawPath(path = electricPath, color = Color.White)
}
타원형 + 그리디언트 + Path 위에 메신저랑 형태가 흡사하다
(추가 작업중)