如何在jetpack compose中使用AsyncImage加载图像并进行灰度转换?

Abh*_*bhi 6 android android-jetpack-compose coil

我无法实现灰度转换。

当前加载图像的代码。

AsyncImage(
    model = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .crossfade(true)
        .build(),
    contentDescription = "",
    contentScale = ContentScale.Crop,
    modifier = Modifier
        .size(44.dp)
        .clip(CircleShape),
)
Run Code Online (Sandbox Code Playgroud)

我发现的灰度转换代码使用了已弃用的代码rememberImagePainter

如何使用rememberAsyncImagePainteror来实现这一点AsyncImage

Gab*_*tti 6

您可以应用该colorFilter属性:

val matrix = ColorMatrix()
matrix.setToSaturation(0F)

AsyncImage(
    model = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .crossfade(true)
        .build(),
    contentDescription = "",
    contentScale = ContentScale.Crop,
    modifier = Modifier
        .size(44.dp)
        .clip(CircleShape),
    colorFilter = ColorFilter.colorMatrix(matrix)
)
Run Code Online (Sandbox Code Playgroud)