Android 中的颜色混合
我正在开发有五种颜色的应用程序:红色,绿色,蓝色,黄色,紫色
我想实现这些颜色的颜色混合:这样每种颜色有五个按钮。
用户触摸该颜色与先前绘制的颜色混合的任何颜色按钮。
我不知道如何添加两种颜色代码并获得第三种颜色。
编辑:
我还必须将此颜色设置为 imageview 的位图,
我该如何设置?
I am working on application in which i have five colors:Red,Green,Blue,Yellow,purple
I want to implement color mixing from those colors:such that like there are five button for each color.
User touch whichever color button this color mix with previously drawn color.
I have not any clue how to add two color codes and get third color.
EDitED:
I have to also set this color to imageview's bitmap
how can i set this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
自 2015 年 4 月起,您可以使用 blendARGB 方法:
int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);
比率值必须为 0.5 以实现均匀混合。
Since April 2015 you can use blendARGB method from v4 support library:
int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);
Ratio value has to be 0.5 to achive even mix.
另一种答案:
您可以混合十六进制中的位:
An alternative answer:
You can mix the bits in the hexs:
SlidingTabStrip 有非常有用的混合颜色的方法,与 ViewPager 一起使用时看起来很棒:
SlidingTabStrip has really useful method for blending colors, looks great when used with ViewPager:
如果颜色在 RGB 空间中,则非常简单(但结果有时不太令人满意):
如果您想使用其他颜色空间,请搜索 Wikipedia 并找到 HSL 颜色空间。您还可以使用一些库来为您做到这一点。
那么你必须阅读这个问题:Calculation of a mix color in RGB
If colors are in RGB space, it is pretty simple (but the result is sometimes not that satisfying):
If you want to use other color spaces, search Wikipedia and find HSL color space. You also have some libraries to do that for you.
Then you will have to read this question: Calculation of a mixed color in RGB
请参阅 ArgbEvaluator(自 API 11 起)
http://developer.android.com/reference/android/animation/ArgbEvaluator.html
See ArgbEvaluator (since API 11)
http://developer.android.com/reference/android/animation/ArgbEvaluator.html
如果您想混合两种颜色(前景和背景),此示例可能很有用。基于 Orlando Leite answare 和维基百科 http://en.wikipedia.org/wiki/Alpha_compositing,使用 Alpha 混合两种颜色的正确方法是:
在这种情况下,Alpha 通道用于计算混合中 RGB 份额的百分比。仅当前景 alpha 小于 100% 时背景颜色才可见
This example may by useful if you want blend two colors (foreground and background). Based on Orlando Leite answare and wikipedia http://en.wikipedia.org/wiki/Alpha_compositing, proper way to blend two colors with alpha is:
In this case alpha channel is used to compute percentage RGB share in blending. Background color may be visible only i foreground alpha is smaller than 100%
在 Android 中,您可以使用
Color
类来处理颜色。通过此类,您可以访问颜色的红色、绿色和蓝色分量,以便您可以对它们执行操作并应用颜色算法。您可以通过以下方式从 color int 中提取颜色分量:
每个值必须在 0 到 255 之间,因此当您将两种颜色混合在一起时,您应该将该值除以二,以确保最终结果在相同的范围内间隔,或者应用另一种算法,记住每个颜色分量对于像素的亮度具有不同的权重。
In Android you can work with Colors using the
Color
class.With this class, you can access the Red, Green and Blue components of a color, so that you can then perform operations with them and apply color algorithms. You can extract the color components from a color int in this way:
Each value must be between 0 and 255, so when you mix two colors together, you should either divide the value by two, to make sure the final result is within the same interval, or apply another algorithm bearing in mind the fact that each color component has a different weight for the luminance of a pixel.
在前面的答案中,“amount2”计算错误。应该是:
所以,整个代码将如下所示:
In previous answer "amount2" is calculated wrongly. Should be:
So, the whole code will look like: