Skip to content

Instantly share code, notes, and snippets.

@L10n42
Created May 4, 2024 13:23
Show Gist options
  • Save L10n42/5f2e87e11750c4d3a88d9a96ee1f6ea9 to your computer and use it in GitHub Desktop.
Save L10n42/5f2e87e11750c4d3a88d9a96ee1f6ea9 to your computer and use it in GitHub Desktop.
Drop Shadow Modifier in Jetpack Compose
import android.graphics.BlurMaskFilter
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawOutline
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
/**
* Adds a drop shadow effect to the composable.
*
* This modifier allows you to draw a shadow behind the composable with various customization options.
*
* @param shape The shape of the shadow.
* @param color The color of the shadow.
* @param blur The blur radius of the shadow
* @param offsetY The shadow offset along the Y-axis.
* @param offsetX The shadow offset along the X-axis.
* @param spread The amount to increase the size of the shadow.
*
* @return A new `Modifier` with the drop shadow effect applied.
*/
fun Modifier.dropShadow(
shape: Shape,
color: Color = Color.Black.copy(0.25f),
blur: Dp = 4.dp,
offsetY: Dp = 4.dp,
offsetX: Dp = 0.dp,
spread: Dp = 0.dp
) = this.drawBehind {
val shadowSize = Size(size.width + spread.toPx(), size.height + spread.toPx())
val shadowOutline = shape.createOutline(shadowSize, layoutDirection, this)
val paint = Paint()
paint.color = color
if (blur.toPx() > 0) {
paint.asFrameworkPaint().apply {
maskFilter = BlurMaskFilter(blur.toPx(), BlurMaskFilter.Blur.NORMAL)
}
}
drawIntoCanvas { canvas ->
canvas.save()
canvas.translate(offsetX.toPx(), offsetY.toPx())
canvas.drawOutline(shadowOutline, paint)
canvas.restore()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment