How can I change the color of my text based on the color of the ImageView it overlays?(如何根据它覆盖的 ImageView 的颜色更改文本的颜色?)
问题描述
所以我在用户上传的 ImageView
上设置了带有白色文本标签的透明按钮.如果用户上传的图片大部分是白色的,那么按钮如果不是完全不可见就很难看到.
有谁知道获取 ImageView
的源图片/drawable 的平均颜色的方法吗?如果我能做到这一点,我可以将它与我可以反复试验的某个阈值进行比较......如果我能得到这个,那么我可以将按钮上的文本颜色更改为这种颜色的反转版本...还是什么??
只是想吐在这里..
当然,如果有人知道更好的方法,将不胜感激更多信息,谢谢!!
你可以使用
So I've got transparant buttons with white text labels set up over a user uploaded ImageView
. If the user uploads an image that is mostly white, then the buttons are hard to see if not completely invisible.
Does anyone know of a way to get the average color of a ImageView
's source picture/drawable? If I can do this, I can compare it to a certain threshold I can trial and error for... If I can get this, then I can change the color of the text on my buttons to the inverted version of this color...or something??
Just idea spitting here..
And of course, if someone knows a better way, more info would be much appreciated, thanks!!
You could use the Palette class.
From the developers guide:
You can retrieve the prominent colors from the image using the getter methods in the
Palette
class, such asPalette.getVibrantColor()
.
Palette.from()
expects a Bitmap
param, so you'll have to get it from your ImageView
.
Something like this should work:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();
And then you can call the appropriate methods on this Palette
instance to get the prominent colors, for example:
int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);
Check out this example screenshot on how the Palette
class recognizes colors:
这篇关于如何根据它覆盖的 ImageView 的颜色更改文本的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何根据它覆盖的 ImageView 的颜色更改文本的颜色?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01