Last active
January 30, 2024 11:29
-
-
Save shakir915/81ffe1423c6cbdac27d01a5b85909cce to your computer and use it in GitHub Desktop.
dropShadow.kt
This file contains hidden or 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
import android.graphics.BlurMaskFilter | |
import androidx.compose.ui.Modifier | |
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.shadow2( | |
color: Color = Color(0xB3DADADA), | |
offsetX: Dp = -1.dp, | |
offsetY: Dp = 1.dp, | |
blurRadius: Dp = 30.dp, | |
) = then( | |
drawBehind { | |
drawIntoCanvas { canvas -> | |
val paint = Paint() | |
val frameworkPaint = paint.asFrameworkPaint() | |
if (blurRadius != 0.dp) { | |
frameworkPaint.maskFilter = (BlurMaskFilter(blurRadius.toPx(), BlurMaskFilter.Blur.NORMAL)) | |
} | |
frameworkPaint.color = color.toArgb() | |
val leftPixel = offsetX.toPx() | |
val topPixel = offsetY.toPx() | |
val rightPixel = size.width + topPixel | |
val bottomPixel = size.height + leftPixel | |
canvas.drawRect( | |
left = leftPixel, | |
top = topPixel, | |
right = rightPixel, | |
bottom = bottomPixel, | |
paint = paint, | |
) | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment