android - 如何根据它覆盖的 ImageView 的颜色更改文本的颜色?

标签 android android-layout user-interface imageview color-detection

所以我在用户上传的 ImageView 上设置了带有白色文本标签的透明按钮。如果用户上传的图片大部分是白色的,那么即使不是完全不可见,也很难看到按钮。

有谁知道获取 ImageView 的源图片/drawable 的平均颜色的方法吗?如果我能做到这一点,我可以将它与我可以反复试验的某个阈值进行比较......如果我能做到这一点,那么我可以将按钮上文本的颜色更改为这种颜色的倒置版本...... .或者什么??

只是想在这里随地吐痰..

当然,如果有人知道更好的方法,将不胜感激,谢谢!!

最佳答案

您可以使用 Palette类。

来自developers guide :

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor().

Palette.from() 需要一个 Bitmap 参数,因此您必须从 ImageView 中获取它。

像这样的东西应该可以工作:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();

然后您可以在此 Palette 实例上调用适当的方法来获取突出的颜色,例如:

int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);

查看此示例屏幕截图,了解 Palette 类如何识别颜色:

enter image description here

关于android - 如何根据它覆盖的 ImageView 的颜色更改文本的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216167/

相关文章:

javascript - 尝试嵌套 React 组件

javascript - double 64 位为什么要在末尾附加零?

java - Android 如何查看我的 SQLite?

java - API 级别 28 的 Android-ProviderTestCase2 不工作

android - 什么是 Android VpnService.Builder setBlocking

javascript - 在源 Pane 中隐藏谷歌开发工具上的空白行

java - Android 错误锁定模式监听器

android - 隐藏基于属性的android textview

Android:如何在中心显示全屏进度条

java - 绘制、重绘、paintComponent