-
-
Save KatieBarnett/8859f927f43d6012a75fc8668fbd501e to your computer and use it in GitHub Desktop.
Zig Zag Line
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
@Composable | |
fun ZigZagLine(color: Color, modifier: Modifier = Modifier) { | |
val shapeWidth = LINE_WIDTH | |
val density = LocalDensity.current | |
val zigZagPath = remember { | |
with(density) { | |
Path().apply { | |
val zigZagWidth = shapeWidth.toPx() | |
val zigZagHeight = shapeWidth.toPx() | |
val zigZagLineWidth = (1.dp).toPx() | |
val shapeVerticalOffset = (zigZagHeight / 2) / 2 | |
val shapeHorizontalOffset = (zigZagHeight / 2) / 2 | |
moveTo(0f, 0f) | |
lineTo(zigZagWidth / 2, zigZagHeight / 2) | |
lineTo(zigZagWidth, 0f) | |
lineTo(zigZagWidth, 0f + zigZagLineWidth) | |
lineTo(zigZagWidth / 2, zigZagHeight / 2 + zigZagLineWidth) | |
lineTo(0f, 0f + zigZagLineWidth) | |
translate(Offset(-shapeHorizontalOffset, -shapeVerticalOffset)) | |
} | |
} | |
} | |
Canvas(modifier) { | |
val pathEffect = PathEffect.stampedPathEffect( | |
shape = zigZagPath, | |
advance = shapeWidth.toPx(), | |
phase = 0f, | |
style = StampedPathEffectStyle.Translate | |
) | |
drawLine( | |
color = color, | |
start = Offset(0f, 0f), | |
end = Offset(size.width, 0f), | |
pathEffect = pathEffect, | |
strokeWidth = shapeWidth.toPx() | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment