Skip to content

Instantly share code, notes, and snippets.

@AidaIssayeva
Last active August 4, 2023 03:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AidaIssayeva/6646ed99c24ddbb79d883ecd50853590 to your computer and use it in GitHub Desktop.
Save AidaIssayeva/6646ed99c24ddbb79d883ecd50853590 to your computer and use it in GitHub Desktop.
Custom Shapes in Jetpack Compose: Heart
@Composable
fun heart(): GenericShape {
return GenericShape { size, _ ->
heartPath(size = size)
}
}
class Heart: Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
val path = Path().apply {
heartPath(size = size)
close()
}
return Outline.Generic(path)
}
}
fun Path.heartPath(size: Size): Path {
//the logic is taken from StackOverFlow [answer](https://stackoverflow.com/a/41251829/5348665)and converted into extension function
val width: Float = size.width
val height: Float = size.height
// Starting point
moveTo(width / 2, height / 5)
// Upper left path
cubicTo(
5 * width / 14, 0f,
0f, height / 15,
width / 28, 2 * height / 5
)
// Lower left path
cubicTo(
width / 14, 2 * height / 3,
3 * width / 7, 5 * height / 6,
width / 2, height
)
// Lower right path
cubicTo(
4 * width / 7, 5 * height / 6,
13 * width / 14, 2 * height / 3,
27 * width / 28, 2 * height / 5
)
// Upper right path
cubicTo(
width, height / 15,
9 * width / 14, 0f,
width / 2, height / 5
)
return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment