Skip to content

Instantly share code, notes, and snippets.

@ShadyRover
Forked from arthurgonzaga/ColoredShadow.kt
Created August 2, 2022 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShadyRover/ef6fa65fe33321436321924ebf1c7797 to your computer and use it in GitHub Desktop.
Save ShadyRover/ef6fa65fe33321436321924ebf1c7797 to your computer and use it in GitHub Desktop.
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.unit.Dp
import androidx.compose.ui.unit.dp
fun Modifier.coloredShadow(
color: Color,
alpha: Float = 0.2f,
borderRadius: Dp = 0.dp,
shadowRadius: Dp = 20.dp,
offsetY: Dp = 0.dp,
offsetX: Dp = 0.dp
) = composed {
val shadowColor = color.copy(alpha = alpha).toArgb()
val transparent = color.copy(alpha= 0f).toArgb()
this.drawBehind {
this.drawIntoCanvas {
val paint = Paint()
val frameworkPaint = paint.asFrameworkPaint()
frameworkPaint.color = transparent
frameworkPaint.setShadowLayer(
shadowRadius.toPx(),
offsetX.toPx(),
offsetY.toPx(),
shadowColor
)
it.drawRoundRect(
0f,
0f,
this.size.width,
this.size.height,
borderRadius.toPx(),
borderRadius.toPx(),
paint
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment