chi*_*dev 11 android colors mixing
我正在申请我有五种颜色:红色,绿色,蓝色,黄色,紫色
我想用这些颜色实现颜色混合:就像每种颜色有五个按钮一样.
用户触摸此颜色与先前绘制的颜色混合的任何颜色按钮.
我不知道如何添加两个颜色代码并获得第三种颜色.
编辑:
我还必须将此颜色设置为imageview的位图
我怎么设置这个?
Szy*_*iak 23
自2015年4月起,您可以使用v4支持库中的blendARGB方法:
int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);
比率值必须为0.5才能达到均匀混合.
Mar*_*rqs 16
SlidingTabStrip具有非常有用的混合颜色方法,与ViewPager一起使用时效果很好:
private static int blendColors(int color1, int color2, float ratio) {
    final float inverseRation = 1f - ratio;
    float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
    float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
    float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
    return Color.rgb((int) r, (int) g, (int) b);
}
Rat*_*ata 12
另一种答案:
您可以混合十六进制中的位:
public static int mixTwoColors( int color1, int color2, float amount )
{
    final byte ALPHA_CHANNEL = 24;
    final byte RED_CHANNEL   = 16;
    final byte GREEN_CHANNEL =  8;
    final byte BLUE_CHANNEL  =  0;
    final float inverseAmount = 1.0f - amount;
    int a = ((int)(((float)(color1 >> ALPHA_CHANNEL & 0xff )*amount) +
                   ((float)(color2 >> ALPHA_CHANNEL & 0xff )*inverseAmount))) & 0xff;
    int r = ((int)(((float)(color1 >> RED_CHANNEL & 0xff )*amount) +
                   ((float)(color2 >> RED_CHANNEL & 0xff )*inverseAmount))) & 0xff;
    int g = ((int)(((float)(color1 >> GREEN_CHANNEL & 0xff )*amount) +
                   ((float)(color2 >> GREEN_CHANNEL & 0xff )*inverseAmount))) & 0xff;
    int b = ((int)(((float)(color1 & 0xff )*amount) +
                   ((float)(color2 & 0xff )*inverseAmount))) & 0xff;
    return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
}
小智 6
请参阅ArgbEvaluator(自API 11起) http://developer.android.com/reference/android/animation/ArgbEvaluator.html
如果颜色在 RGB 空间中,则非常简单(但结果有时并不那么令人满意):
public int mixColors(int col1, int col2) {
    int r1, g1, b1, r2, g2, b2;
    r1 = Color.red(col1);
    g1 = Color.green(col1);
    b1 = Color.blue(col1);
    r2 = Color.red(col2);
    g2 = Color.green(col2);
    b2 = Color.blue(col2);
    int r3 = (r1 + r2)/2;
    int g3 = (g1 + g2)/2;
    int b3 = (b1 + b2)/2;
    return Color.rgb(r3, g3, b3);
}
如果您想使用其他颜色空间,请搜索维基百科并找到 HSL 颜色空间。您还有一些库可以为您执行此操作。
那么你将不得不阅读这个问题:RGB 中混合颜色的计算
| 归档时间: | 
 | 
| 查看次数: | 14633 次 | 
| 最近记录: |