Created
May 7, 2024 00:32
-
-
Save victory316/cb4b03a724022622036e54eb0028f2f1 to your computer and use it in GitHub Desktop.
Jetpack compose colorFilter sample to make image dimmed in condition.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Reference: https://stackoverflow.com/a/78230014 | |
*/ | |
@Composable | |
fun ColorFilterSample(isActive: Boolean, iconResId: Int) { | |
val brightness = if (isActive) 0f else 0f | |
val contrast = if (isActive) 1f else 0.5f | |
val saturation = if (isActive) 1f else 0.7f | |
val filter = ColorFilter.colorMatrix( | |
ColorMatrix( | |
floatArrayOf( | |
contrast, 0f, 0f, 0f, brightness, | |
0f, contrast, 0f, 0f, brightness, | |
0f, 0f, contrast, 0f, brightness, | |
0f, 0f, 0f, 1f, 0f | |
) | |
).apply { | |
val saturationMatrix = ColorMatrix() | |
saturationMatrix.setToSaturation(saturation) | |
timesAssign(saturationMatrix) | |
} | |
) | |
Image( | |
painter = painterResource(iconResId), | |
colorFilter = filter, | |
contentDescription = null | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment