How to change color Image into black/white in jetpack compose

Viewed 4319

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

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

Image(
        painter = painter, contentDescription = null,
        modifier = Modifier
            .fillMaxSize(),
        contentScale = scale,
        colorFilter = ColorFilter.tint(color = MaterialTheme.colors.onBackground)
    )

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)
    )
Related