Skip to content

Instantly share code, notes, and snippets.

@hoangchungk53qx1
Created March 3, 2024 13:56
Show Gist options
  • Select an option

  • Save hoangchungk53qx1/ddc7c276d83fb674da432fd283f7f89a to your computer and use it in GitHub Desktop.

Select an option

Save hoangchungk53qx1/ddc7c276d83fb674da432fd283f7f89a to your computer and use it in GitHub Desktop.
CornerOverlay
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.ClipOp
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
fun CornerOverlay() {
Canvas(modifier = Modifier.fillMaxSize()) {
val rectWidth = size.width * 0.7f
val rectHeight = size.width * 0.7f
val left = (size.width - rectWidth) / 2
val top = (size.height - rectHeight) / 2
val right = left + rectWidth
val bottom = top + rectHeight
val alpha = 0.5f
val borderColor = Color.White
val borderWidth = 4.dp
val cornerRadius = 20.dp
val rect0 = Rect(left, top, right, bottom)
val path = Path().apply {
addRect(rect0)
}
clipPath(path) {
drawRect(SolidColor(Color.Black.copy(alpha = alpha)))
}
clipPath(
Path().apply {
addRect(rect0)
addRoundRect(RoundRect(left, top, right, bottom, cornerRadius = CornerRadius(cornerRadius.toPx())))
},
clipOp = ClipOp.Difference,
) {
drawRect(SolidColor(Color.Black.copy(alpha = alpha)))
}
drawRoundRect(
color = borderColor,
style = Stroke(width = borderWidth.toPx()),
topLeft = Offset(left, top),
size = Size(rectWidth, rectHeight),
cornerRadius = CornerRadius(cornerRadius.toPx()),
)
}
}
@Preview
@Composable
fun CornerOverlayPreview() {
CornerOverlay()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment