22

I want to show color image in black/white, how to do it using colorFilter in android jetpack compose?

    Image(
        painter = painter, contentDescription = null,
        modifier = Modifier
            .fillMaxSize(),
        contentScale = scale,
        colorFilter = null
    )

2 Answers 2

35

You can also use ColorFilter.tint(color = ...)

Image(
        painter = painter, contentDescription = null,
        modifier = Modifier
            .fillMaxSize(),
        contentScale = scale,
        colorFilter = ColorFilter.tint(color = MaterialTheme.colors.onBackground)
    )
1
  • 1
    Well, this will only result in grayscaling if you didn't modify the default theme. ;) Better use Color.Black instead of MaterialTheme.colors.onBackground/MaterialTheme.colorScheme.onBackground. Oct 7, 2022 at 14:41
18

We can easily achieve that by using colorMatrix:

    val matrix = ColorMatrix()
    matrix.setToSaturation(0F)
    
    Image(
        painter = painter, contentDescription = null,
        modifier = Modifier
            .fillMaxSize(),
        contentScale = scale,
        colorFilter = ColorFilter.colorMatrix(matrix)
    )
2

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.