# Random Animating Pie Button

DevFeed: [Random Animating Pie Button](<https://devfeed.tech/articles/random-animating-pie-button-32075.md>)

Original publisher: [Read original article](<https://www.maiatoday.net/p/random-animating-pie-button/>)

Published: 2021-06-16T21:36:11Z

Content type: tutorial

Language: en

Sources: [maiatoday](<https://devfeed.tech/sources/maiatoday.md>)

Topics: [Compose](<https://devfeed.tech/topics/compose.md>), [Canvas](<https://devfeed.tech/topics/canvas.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [animate](<https://devfeed.tech/tags/animate.md>), [canvas](<https://devfeed.tech/tags/canvas.md>), [code](<https://devfeed.tech/tags/code.md>), [color](<https://devfeed.tech/tags/color.md>), [component](<https://devfeed.tech/tags/component.md>), [compose](<https://devfeed.tech/tags/compose.md>), [custom](<https://devfeed.tech/tags/custom.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [exploring](<https://devfeed.tech/tags/exploring.md>), [fun](<https://devfeed.tech/tags/fun.md>), [functions](<https://devfeed.tech/tags/functions.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [random](<https://devfeed.tech/tags/random.md>), [val](<https://devfeed.tech/tags/val.md>), [var](<https://devfeed.tech/tags/var.md>)

## AI overview

A Jetpack Compose sample demonstrates a custom pie-chart component that draws with Canvas and animates to a random percentage when a button is clicked.

## Source excerpt

I am exploring animations with small sampler functions using Jetpack Compose. This one is a custom component that draws a little pie chart. It will animate a random pie value on the click of the button. data class PieData( val foreground: Color = Color.White, val strokeWidth: Dp = 4.dp, val percentage: Float ) @Composable fun PieStatus( modifier: Modifier = Modifier, pieData: PieData ) { var animationPlayed by remember { mutableStateOf(false) } val currentPercentage = animateFloatAsState( targetValue = if (animationPlayed) pieData.percentage else 0f, animationSpec = tween(1000) ) LaunchedEffect(key1 = true) { animationPlayed = true } Canvas( modifier = modifier ) { val canvasWidth = size.width val canvasHeight = size.height drawCircle( color = pieData.foreground, center = Offset(x = canvasWidth / 2, y = canvasHeight / 2), radius = canvasWidth / 2 - pieData.strokeWidth.toPx(), style = Stroke(width = pieData.strokeWidth.toPx()) ) val arcPadding = pieData.strokeWidth.toPx() * 2 drawArc( color = pieData.foreground, startAngle = -90f, sweepAngle = currentPercentage.value * 360, useCenter = true, topLeft = Offset(arcPadding, arcPadding), size = Size(size.width - (arcPadding * 2f), size.height - (arcPadding * 2f)) ) } } code