Skip to content

Instantly share code, notes, and snippets.

@theapache64
Created October 28, 2021 13:21
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 theapache64/db627f8fb4496da2e1fce94bd9036a76 to your computer and use it in GitHub Desktop.
Save theapache64/db627f8fb4496da2e1fce94bd9036a76 to your computer and use it in GitHub Desktop.
private val BottomRectangle = GenericShape { size, _ ->
// 1)
moveTo(0f, size.height * 0.6f)
// 2)
lineTo(size.width, size.height * 0.6f)
lineTo(size.width, size.height)
lineTo(0f, size.height)
// 3)
lineTo(0f, size.height * 0.6f)
}
/*
@Composable
fun SampleImage() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
) {
Image(
painter = painterResource(id = R.drawable.movie_poster),
contentDescription = "",
modifier = Modifier
.fillMaxWidth()
.height(300.dp),
contentScale = ContentScale.Crop
)
Column(
modifier = Modifier.blur(
30.dp,
edgeTreatment = BlurredEdgeTreatment(BottomRectangle)
)
)
{
Image(
painter = painterResource(id = R.drawable.movie_poster),
contentDescription = "",
contentScale = ContentScale.Crop,
modifier = Modifier
.fillMaxWidth()
.drawWithContent {
this.drawContext.canvas.nativeCanvas.
}
.height(300.dp)
)
Box(modifier = Modifier.background(Color.Black).fillMaxWidth().height(100.dp))
}
}
}*/
fun Modifier.customBlur(
radiusX: Dp,
radiusY: Dp,
edgeTreatment: BlurredEdgeTreatment = BlurredEdgeTreatment.Rectangle,
): Modifier {
val clip: Boolean
val tileMode: TileMode
if (edgeTreatment.shape != null) {
clip = true
tileMode = TileMode.Clamp
} else {
clip = true
tileMode = TileMode.Decal
}
return if ((radiusX > 0.dp && radiusY > 0.dp) || clip) {
graphicsLayer {
val horizontalBlurPixels = radiusX.toPx()
val verticalBlurPixels = radiusY.toPx()
this.renderEffect =
// Only non-zero blur radii are valid BlurEffect parameters
if (horizontalBlurPixels > 0f && verticalBlurPixels > 0f) {
BlurEffect(horizontalBlurPixels, verticalBlurPixels, tileMode)
} else {
null
}
this.shape = edgeTreatment.shape ?: RectangleShape
this.clip = true
}
} else {
this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment