Skip to content

Instantly share code, notes, and snippets.

@AndreVero
Created February 24, 2024 13:56
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 AndreVero/d51eb0404c34d71f3047e16931e093d8 to your computer and use it in GitHub Desktop.
Save AndreVero/d51eb0404c34d71f3047e16931e093d8 to your computer and use it in GitHub Desktop.
Drawing ui in extension function
fun DrawScope.drawCircleInfo(
item: GlovoItem,
innerCircleRadius: Dp,
iconScale: Float,
textStyle: TextStyle,
textMeasurer: TextMeasurer,
animationValue: Float,
currentOffset: Offset
) {
// Draw secondary items with counted offset
drawCircle(
color = Color.White,
radius = innerCircleRadius.toPx() * animationValue,
center = currentOffset
)
// Get the path bounds to translate path exactly to the center of the circle
val pathBounds = item.path.getBounds()
// Move our path to the new position
translate(
left = currentOffset.x - (pathBounds.right * iconScale) / 2,
top = currentOffset.y - 15.dp.toPx() - pathBounds.bottom
) {
// Increase path in 3 times
scale(scale = iconScale * animationValue, pivot = pathBounds.topLeft) {
drawPath(
path = item.path,
color = Color.Black
)
}
}
// Calculate all needed info for text drawing
val measurementResult = textMeasurer.measure(
item.title,
constraints = Constraints(
// Set constraints to prevent overflow
maxWidth = (innerCircleRadius.toPx() * 2 - 16.dp.toPx()).toInt()
),
// Set style for the text
style = textStyle.copy(
textAlign = TextAlign.Center,
fontSize = textStyle.fontSize * animationValue
)
)
drawText(
textLayoutResult = measurementResult,
// For text we need to set top left corner from which drawing will be started
// in this case I set text in the middle
topLeft = Offset(
x = currentOffset.x - measurementResult.size.width / 2,
y = currentOffset.y + pathBounds.height
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment