I use Coil
along with Compose
.
And I'm trying to make a shimmer animation while the image is loading.
All examples use ImagePainter
with ImagePainter.State
and it works correctly, but this is now marked as "deprecated".
That's why I chose AsyncImagePainter
. Without the state check it works perfect, but with the check I get an infinite shimmer animation.
I also tried to change the load state var with mutableState in the onSuccess method in AsyncImagePainter
, but the animation is still infinite
@Composable
fun FoodItem(food: Fun) {
Column (
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Top
) {
val painter = rememberAsyncImagePainter(food.image.sm)
if (painter.state is AsyncImagePainter.State.Loading) {
AnimatedShimmer { ShimmerFoodItem(brush = it) }
} else {
Image(
modifier = Modifier
.fillMaxWidth()
.height(104.dp)
.clip(RoundedCornerShape(size = 8.dp)),
painter = painter,
contentScale = ContentScale.Crop,
contentDescription = "Food photo"
)
Text(
modifier = Modifier.padding(top = 4.dp),
text = food.title,
fontSize = MaterialTheme.typography.body1.fontSize,
overflow = TextOverflow.Ellipsis,
maxLines = 1,
fontWeight = FontWeight.Bold
)
Text(
text = food.subtitle,
fontSize = MaterialTheme.typography.subtitle2.fontSize,
overflow = TextOverflow.Ellipsis,
maxLines = 2,
fontWeight = FontWeight.Normal
)
}
}
}