android - 如何在android compose中以黑白显示图像
问题描述
我正在尝试使用 android compose 将显示的彩色图像转换为黑白。
在视图系统中,我可以通过添加这样的过滤器将图像从彩色更改为黑白
imageView.colorFilter = ColorMatrixColorFilter(ColorMatrix().apply { setSaturation(0f)})
如this answer所示。
在 Android Compose 中,Image可组合函数已经采用了颜色过滤器,但我在 compose 包中 找不到等效的ColorMatrixColorFilter 。
这是我要转换为灰度的图像代码
Image(
asset = vectorResource(id = R.drawable.xxx),
modifier = Modifier.clip(RectangleShape).size(36.dp, 26.dp),
alpha = alpha,
alignment = Alignment.Center,
contentScale = ContentScale.Fit
)
解决方案
我希望我没有误解这个问题,但这帮助我将图像转换为灰度。这是根据当前的 Compose 版本 1.0.0-beta01
val grayScaleMatrix = ColorMatrix(
floatArrayOf(
0.33f, 0.33f, 0.33f, 0f, 0f,
0.33f, 0.33f, 0.33f, 0f, 0f,
0.33f, 0.33f, 0.33f, 0f, 0f,
0f, 0f, 0f, 1f, 0f
)
)
Image(
painter = painterResource(id = imageId),
contentDescription = "",
colorFilter = ColorFilter.colorMatrix(matrix)
)
推荐阅读
- kotlin - Kotlin:如何限制块内的范围?
- node.js - 如何在 Firebase 上使用已部署的应用程序和已部署的函数存储数据
- html - Bootstrap 4 nav-tabs 下拉项目不通过活动标签面板切换
- actions-on-google - 针对网站而非应用的 Google 品牌名称所有权验证操作
- c# - 如何将 C# 中的日期从一种格式转换为另一种格式?
- c - 指针下标从负值开始
- javascript - 共享工作者 - 我可以拥有不同组的同一个共享工作者吗?
- ios - 是否可以使用 Windows 10 和 React Native 创建 iOS 应用程序 (.ipa)?
- r - data.table 中的条件滚动字符串 concat
- html - Angular HTML 模板最佳实践