Skip to content

Instantly share code, notes, and snippets.

@hernandazevedo
Last active March 30, 2023 14:00
Show Gist options
  • Save hernandazevedo/dfd41b39d0156c740a195f6f5866ce20 to your computer and use it in GitHub Desktop.
Save hernandazevedo/dfd41b39d0156c740a195f6f5866ce20 to your computer and use it in GitHub Desktop.
internal fun Modifier.coloredShadow(
color: Color = Color.Black,
borderRadius: Dp = 0.dp,
blurRadius: Dp = 0.dp,
offsetY: Dp = 0.dp,
offsetX: Dp = 0.dp,
spread: Float = 0f,
modifier: Modifier = Modifier,
) = this.then(
modifier.drawBehind {
this.drawIntoCanvas {
val paint = Paint()
val frameworkPaint = paint.asFrameworkPaint()
val spreadPixel = spread.dp.toPx()
val leftPixel = (0f - spreadPixel) + offsetX.toPx()
val topPixel = (0f - spreadPixel) + offsetY.toPx()
val rightPixel = (this.size.width + spreadPixel)
val bottomPixel = (this.size.height + spreadPixel)
if (blurRadius != 0.dp) {
/*
The feature maskFilter used below to apply the blur effect only works
with hardware acceleration disabled.
*/
frameworkPaint.maskFilter =
(BlurMaskFilter(blurRadius.toPx(), BlurMaskFilter.Blur.NORMAL))
}
frameworkPaint.color = color.toArgb()
it.drawRoundRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
radiusX = borderRadius.toPx(),
radiusY = borderRadius.toPx(),
paint
)
}
}
)
private fun DrawScope.clipToRadius(cornerRadius: Double) {
this.drawIntoCanvas {
val path = Path()
path.addRoundRect(RoundRect(left = 0f,
top = 0f,
right = (this.size.width),
bottom = (this.size.height),
cornerRadius = CornerRadius(cornerRadius.dp.toPx(), cornerRadius.dp.toPx())))
it.clipPath(path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment