How to load image using AsyncImage in jetpack compose with GrayScale transformation?
Asked Answered
W

1

6

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?

Wreathe answered 21/8, 2022 at 18:26 Comment(0)
S
8

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)
)
Sheena answered 21/8, 2022 at 20:45 Comment(1)
Worked perfectly, thanks!Principate

© 2022 - 2024 — McMap. All rights reserved.