Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD/#28] CustomShadow 구현 #29

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {

// Compose Preview
debugImplementation(libs.compose.ui.tooling)
implementation(libs.androidx.ui.tooling.preview)

// Test Dependency
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.terning.core.designsystem.component.modifier

import android.graphics.BlurMaskFilter
import androidx.compose.runtime.Composable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifier의 확장함수면 core>extension에 있는 Modifier.kt 파일에 넣는 게 좋을 것 같아요!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어머어머 세상에 Modifier 이미 있는거 깜빡했네요😅 감사해용!!

import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.terning.core.designsystem.theme.Grey200

@Composable
fun Modifier.customShadow(
color: Color = Grey200,
shadowRadius: Dp = 0.dp,
shadowWidth: Dp = 2.dp,
offsetX: Dp = 0.dp,
offsetY: Dp = 0.dp,
) = composed {
val paint: Paint = remember { Paint() }
val density = LocalDensity.current
val radius = shadowRadius + shadowWidth
val blurRadiusPx = with(density) { radius.toPx() }
val maskFilter = remember {
BlurMaskFilter(blurRadiusPx, BlurMaskFilter.Blur.NORMAL)
}
drawBehind {
drawIntoCanvas { canvas ->
val frameworkPaint = paint.asFrameworkPaint()
if (radius != 0.dp) {
frameworkPaint.maskFilter = maskFilter
}
frameworkPaint.color = color.toArgb()

val leftPixel = offsetX.toPx()
val topPixel = offsetY.toPx()
val rightPixel = size.width + leftPixel
val bottomPixel = size.height + topPixel

if (radius > 0.dp) {
val radiusPx = radius.toPx()
canvas.drawRoundRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
radiusX = radiusPx,
radiusY = radiusPx,
paint = paint,
)
} else {
canvas.drawRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
paint = paint,
)
}
}
}
}
Loading