In this post I will show you an example of how to create a custom progress indicator in Android using SVG paths.
The examples shown below demonstrate various ways to create a loading animation while a long waited task is being performed. They build on the UI so the user isn't being presented with a blank screen.
They could be adjusted for your desired visual needs.
Custom Progress Indicator In Android
You may tweak and change what you want here. They are just jumping off ideas. When complete, they could be added to a custom personal library. Which would help to incorporate into your Android apps.
Custom Progress Indicators In Android
You may tweak and change what you want here. They are just jumping off ideas. When complete, they could be added to a custom personal library. Which would help to incorporate into your Android apps.
Dynamic Loading Lines
The first example are dynamic line. They bounce back and forth from one side of screen to other. The path they are based on are EKG machine lines.
@Composable
fun MovingLines() {
val progress = remember { Animatable(0f) }
var first : Int = Random().nextInt(10) * 1
var sec : Int = Random().nextInt(10) * -1
var third : Int = Random().nextInt(10) * 1
var colorFlip : Boolean = false;
var androidPath: Path =
PathParser().parsePathString("m0,5 l5,0 l1,${first} l1,${-first} l1,${sec} l1,${-sec} l5,0 l2,${third} l1,${-third} l5,0")
.toPath()
LaunchedEffect(Unit) {
while (true) {
progress.animateTo(
targetValue = 1f,
animationSpec = tween(durationMillis = 2000, easing = LinearEasing)
)
// Runs at the end of every single cycle
first = Random().nextInt(10) * 1
sec = Random().nextInt(10) * -1
third = Random().nextInt(10) * 1
androidPath =
PathParser().parsePathString("m0,5 l5,0 l1,${first} l1,${-first} l1,${sec} l1,${-sec} l5,0 l2,${third} l1,${-third} l5,0")
.toPath()
colorFlip = !colorFlip
progress.snapTo(0f)
}
}
val boxPadding = 16.dp
Row(
modifier = Modifier
.fillMaxWidth(0.5f)
.aspectRatio(1f)
.padding(boxPadding)
.drawWithContent {
val bounds = androidPath.getBounds()
val scaleX = (size.width - boxPadding.toPx() * 2) / bounds.width
val scaleY = (size.height - boxPadding.toPx() * 2) / bounds.height
val scale = min(scaleX, scaleY)
val scaledWidth = (bounds.width * scale)
val scaledHeight = bounds.height * scale
val offset = Offset(
(size.width - scaledWidth) / 2f - (bounds.left * scale),
(size.height - scaledHeight) / 2f - (bounds.top * scale)
)
val matrix = Matrix().apply {
translate(offset.x, offset.y)
scale(scale, scale)
}
androidPath.transform(matrix)
val pm = PathMeasure().apply { setPath(androidPath, false) }
val distance = progress.value * pm.length
val point = pm.getPosition(distance)
val destPath: Path = Path().apply {
}
pm.getSegment(if(distance - pm.length*0.25f < 0f) 0f else distance - pm.length * 0.25f, distance, destPath, true)
rotate(180f, center, {
drawPath(
path = destPath,
brush = Brush.radialGradient(
colorStops = arrayOf(
0.5f to Color.Black,
1.0f to Color.Transparent
),
center = point
),
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
drawPoints(
points = listOf(point),
pointMode = PointMode.Points,
color = if (colorFlip) Color.Red else Color.Blue,
strokeWidth = 64f,
cap = StrokeCap.Round
)
})
drawPath(
path = destPath,
brush = Brush.radialGradient(
colorStops = arrayOf(
0.5f to Color.Black,
1.0f to Color.Transparent
),
center = point
),
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
drawPoints(
points = listOf(point),
pointMode = PointMode.Points,
color = if (colorFlip) Color.Blue else Color.Red,
strokeWidth = 64f,
cap = StrokeCap.Round
)
}, horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically
) { }
}Focusing Lens
The next example simulates a focusing camera lens.
@Composable
fun CameraLens() {
val fgColor = Color.White
val bgColor = Color.Black
val infiniteTransition = rememberInfiniteTransition(label = "path_transition")
val rotation by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 360f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 8000, easing = LinearEasing),
repeatMode = RepeatMode.Restart
),
label = "rotation"
)
val outCircle: Path =
PathParser().parsePathString("M0,50 A50,50 0 0 1 50 0 M100,50 A50,50 0 0 1 50 100")
.toPath()
val boxPadding = 16.dp
val p = 0.5f
Row(
modifier = Modifier
.fillMaxWidth(0.5f)
.aspectRatio(1f)
.padding(boxPadding)
.drawWithContent {
val bounds = outCircle.getBounds()
val scaleX = (size.width - boxPadding.toPx() * 2) / bounds.width
val scaleY = (size.height - boxPadding.toPx() * 2) / bounds.height
val scale = min(scaleX, scaleY)
val scaledWidth = (bounds.width * scale)
val scaledHeight = bounds.height * scale
val offset = Offset(
(size.width - scaledWidth) / 2f - (bounds.left * scale),
(size.height - scaledHeight) / 2f - (bounds.top * scale)
)
var matrix = Matrix().apply {
translate(offset.x, offset.y)
scale(scale, scale)
rotateZ(p)
}
outCircle.transform(matrix)
drawPath(
path = outCircle,
color = bgColor,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
val innerCircle = outCircle.copy()
matrix = Matrix().apply {
translate(outCircle.getBounds().center.x*0.3755f, outCircle.getBounds().center.y*0.375f)
scale(scale*0.625f, scale*0.625f)
}
innerCircle.transform(matrix)
rotate(-rotation, center, {
drawPath(
path = innerCircle,
color = bgColor,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
})
matrix = Matrix().apply {
translate(outCircle.getBounds().center.x*0.5f, outCircle.getBounds().center.y*0.5f)
scale(scale*0.5f, scale*0.5f)
}
innerCircle.transform(matrix)
drawPath(
path = innerCircle,
color = bgColor,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
drawCircle(Color.Blue, innerCircle.getBounds().width/4, center = center)
}, horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically
) { }
}Circle
The third example is a continuous dot rotating around a two toned circle.
@Composable
fun CircleLoading() {
val fgColor = Color.White
val bgColor = Color.Black
var flip by remember { mutableStateOf(false) }
val infiniteTransition = rememberInfiniteTransition(label = "path_transition")
val scaledSize by infiniteTransition.animateFloat(
initialValue = 0.9f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 1000, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
),
label = "scaleSize"
)
val progress = remember { Animatable(0f) }
LaunchedEffect(Unit) {
while (true) {
progress.animateTo(
targetValue = 1f,
animationSpec = tween(durationMillis = 3000, easing = LinearEasing)
)
// Runs at the end of every single cycle
flip = !flip
progress.snapTo(0f)
}
}
var androidPath: Path =
PathParser().parsePathString("m50,50 a50,50 0 1,0 100,0 a50,50 180 1,0 -100,0")
.toPath()
val boxPadding = 16.dp
Row(
modifier = Modifier
.fillMaxWidth(0.5f)
.aspectRatio(1f)
.scale(scaledSize)
.padding(boxPadding)
.drawWithContent {
val bounds = androidPath.getBounds()
val scaleX = (size.width - boxPadding.toPx() * 2) / bounds.width
val scaleY = (size.height - boxPadding.toPx() * 2) / bounds.height
val scale = min(scaleX, scaleY)
val scaledWidth = (bounds.width * scale)
val scaledHeight = bounds.height * scale
val offset = Offset(
(size.width - scaledWidth) / 2f - (bounds.left * scale),
(size.height - scaledHeight) / 2f - (bounds.top * scale)
)
val matrix = Matrix().apply {
translate(offset.x, offset.y)
scale(scale, scale)
}
androidPath.transform(matrix)
drawPath(
path = androidPath,
color = if(!flip) bgColor else fgColor,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
val pm = PathMeasure().apply { setPath(androidPath, false) }
val distance = progress.value * pm.length
val point = pm.getPosition(distance)
val destPath: Path = Path().apply {
}
pm.getSegment(0f, distance, destPath, true)
drawPath(
path = destPath,
color = if(!flip) fgColor else bgColor,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
drawPoints(
points = listOf(point),
pointMode = PointMode.Points,
color = Color.Blue,
strokeWidth = 64f,
cap = StrokeCap.Round
)
}, horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically
) { }
}Swuiggly Lines
Lastly I have a squiggly line with a dot that rides along it. The dot travels from one side to the other.
@Composable
fun SquigglyLine() {
val infiniteTransition = rememberInfiniteTransition(label = "path_transition")
val progress by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 3000, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
),
label = "progress"
)
val bezierL by infiniteTransition.animateFloat(
initialValue = 25f,
targetValue = 75f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 3000, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
),
label = "bezierL"
)
val bezierR by infiniteTransition.animateFloat(
initialValue = 75f,
targetValue = 25f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 3000, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
),
label = "bezierL"
)
val semi by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 100f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 3000, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
),
label = "semi"
)
var androidPath: Path =
PathParser().parsePathString("M10,50 Q25,${bezierL} 50,50 Q75,${bezierR} 90,50")
.toPath()
var androidPathBAK: Path by remember {
mutableStateOf(
Path().apply {
moveTo(10f, 50f)
quadraticTo(25f, bezierL, 50f, 50f)
quadraticTo(75f, bezierR, 90f, 50f)
}
)
}
val boxPadding = 16.dp
Row(
modifier = Modifier
.fillMaxWidth(0.5f)
.aspectRatio(1f)
.padding(boxPadding)
.drawWithContent {
val bounds = androidPath.getBounds()
val scaleX = (size.width - boxPadding.toPx() * 2) / bounds.width
val scaleY = (size.height - boxPadding.toPx() * 2) / bounds.height
val scale = min(scaleX, scaleY)
val scaledWidth = (bounds.width * scale)
val scaledHeight = bounds.height * scale
val offset = Offset(
(size.width - scaledWidth) / 2f - (bounds.left * scale),
(size.height - scaledHeight) / 2f - (bounds.top * scale)
)
val matrix = Matrix().apply {
translate(offset.x, offset.y)
scale(scale, scale)
}
androidPath.transform(matrix)
drawPath(
path = androidPath,
color = Color.Black,
style = Stroke(
width = 32f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
val pm = PathMeasure().apply { setPath(androidPath, false) }
val distance = progress * pm.length
val point = pm.getPosition(distance)
val destPath: Path = Path().apply {
}
pm.getSegment(0f, distance, destPath, true)
drawPath(
path = destPath,
color = Color.White,
style = Stroke(
width = 24f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
)
)
drawPoints(
points = listOf(point),
pointMode = PointMode.Points,
color = Color.Blue,
strokeWidth = 64f,
cap = StrokeCap.Round
)
}, horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically
) { }
}Testing the Composable Methods
Below I have shown a way that each Composable is called into the UI.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
LoadingWithPathsTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Column(modifier = Modifier.fillMaxSize().padding(innerPadding).verticalScroll(rememberScrollState()), horizontalAlignment = Alignment.CenterHorizontally) {
MovingLines()
Box(modifier = Modifier.fillMaxWidth(0.5f).height(6.dp).background(Color.LightGray)){}
CameraLens()
Box(modifier = Modifier.fillMaxWidth(0.5f).height(6.dp).background(Color.LightGray)){}
CircleLoading()
Box(modifier = Modifier.fillMaxWidth(0.5f).height(6.dp).background(Color.LightGray)){}
SquigglyLine()
}
}
}
}
}
}
