Skip to content

Instantly share code, notes, and snippets.

@JesusM
Created February 3, 2022 07:19
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 JesusM/a37f07e191cbf851a972e3e350c24ce2 to your computer and use it in GitHub Desktop.
Save JesusM/a37f07e191cbf851a972e3e350c24ce2 to your computer and use it in GitHub Desktop.
package com.jesusm.passesreader.ui.base
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
class TicketListItemShape(
private val cornerRadius: Float,
) : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
return Outline.Generic(
path = drawPath(
size = size,
cornerRadius = cornerRadius,
)
)
}
private fun drawPath(
size: Size,
cornerRadius: Float
): Path {
val cornerSize = Size(width = cornerRadius * 2, height = cornerRadius * 2)
return Path().apply {
reset()
// Top start arc
arcTo(
rect = Rect(
Offset(x = 0f, y = 0f),
cornerSize
),
startAngleDegrees = 180f,
sweepAngleDegrees = 90f,
forceMoveTo = false
)
lineTo(x = (size.width / 2) - cornerRadius, y = 0f)
arcTo(
rect = Rect(
Offset(x = (size.width / 2) - cornerRadius, y = -cornerRadius),
cornerSize
),
startAngleDegrees = 180.0f,
sweepAngleDegrees = -180.0f,
forceMoveTo = false
)
lineTo(x = size.width - (cornerRadius / 2), y = 0f)
// Top end arc
arcTo(
rect = Rect(
Offset(x = size.width - (cornerRadius * 2), y = 0f),
cornerSize
),
startAngleDegrees = 270f,
sweepAngleDegrees = 90f,
forceMoveTo = false
)
lineTo(x = size.width, y = size.height)
lineTo(x = 0f, y = size.height)
lineTo(x = 0f, y = cornerRadius)
close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment