线圈 RememberAsyncImagePainter 状态未更新

qve*_*eex 9 android kotlin android-jetpack-compose coil

Coil与 一起使用Compose

我正在尝试在加载图像时制作闪烁的动画。
所有示例都使用ImagePainterwithImagePainter.State并且它可以正常工作,但现在已标记为“已弃用”。
这就是我选择的原因AsyncImagePainter。如果没有状态检查,它可以完美工作,但是通过检查,我会得到无限的闪烁动画
我也尝试在onSuccess方法中使用mutableState更改加载状态var AsyncImagePainter,但动画仍然是无限的

@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
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Thr*_*ian 11

您需要使用提供尺寸

model = ImageRequest.Builder(LocalContext.current)
    .data(overlayImage)
    .size(coil.size.Size.ORIGINAL) // Set the target size to load the image at.
    .build()
Run Code Online (Sandbox Code Playgroud)

如果您注释行大小,您会发现它卡在加载状态。您还可以提供您想要加载图像的任何尺寸,例如 1280x720.size(coil.size.Size(1280, 720))

@Composable
private fun CoilSample() {
    val overlayImage =
        "https://images6.alphacoders.com/488/thumb-1920-488158.jpg"

    val painter = rememberAsyncImagePainter(
        model = ImageRequest.Builder(LocalContext.current)
            .data(overlayImage)
            .size(coil.size.Size.ORIGINAL) // Set the target size to load the image at.
            .build()
    )

    Column(modifier = Modifier.fillMaxSize()) {

        Text("state:${painter.state}")

        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            if (painter.state is AsyncImagePainter.State.Loading) {
                CircularProgressIndicator()
            } else {
                Image(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(104.dp)
                        .clip(RoundedCornerShape(size = 8.dp)),
                    painter = painter,
                    contentScale = ContentScale.Crop,
                    contentDescription = "photo"
                )
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要从 Coil 画家那里获取Bitmap或获取,您也可以查看此答案ImageBitmap

在可组合项中使用之前获取位图