4

I am unable to achieve the grayscale transformation.

Current code to load Image.

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

The code I found for GrayScale transformation uses rememberImagePainter which is deprecated.

How to achieve this using rememberAsyncImagePainter or AsyncImage?

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121

1 Answers1

4

You can apply the colorFilter attribute:

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)
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841