Created
November 17, 2022 14:09
-
-
Save mcatta/dd9e0b2fa65f8642ce01b8adabc72f84 to your computer and use it in GitHub Desktop.
A Jetpack Compose Modifier which allows to add a colored shadow with a specific X and Y offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Change shadow color and style | |
* @param color shadow color | |
* @param alpha shadow alpha | |
* @param borderRadius shadow border radius | |
* @param shadowRadius shadow size | |
* @param offsetX X offset | |
* @param offsetY Y offset | |
*/ | |
@RequiresApi(Build.VERSION_CODES.P) | |
private 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