所以我在用户上传的 ImageView
上设置了带有白色文本标签的透明按钮。如果用户上传的图片大部分是白色的,那么即使不是完全不可见,也很难看到按钮。
有谁知道获取 ImageView
的源图片/drawable 的平均颜色的方法吗?如果我能做到这一点,我可以将它与我可以反复试验的某个阈值进行比较......如果我能做到这一点,那么我可以将按钮上文本的颜色更改为这种颜色的倒置版本...... .或者什么??
只是想在这里随地吐痰..
当然,如果有人知道更好的方法,将不胜感激,谢谢!!
最佳答案
您可以使用 Palette类。
来自developers guide :
You can retrieve the prominent colors from the image using the getter methods in the
Palette
class, such asPalette.getVibrantColor()
.
Palette.from()
需要一个 Bitmap
参数,因此您必须从 ImageView
中获取它。
像这样的东西应该可以工作:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();
然后您可以在此 Palette
实例上调用适当的方法来获取突出的颜色,例如:
int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);
查看此示例屏幕截图,了解 Palette
类如何识别颜色:
关于android - 如何根据它覆盖的 ImageView 的颜色更改文本的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216167/