“CALENDAR_COLOR”中的Android日历颜色与实际日历颜色不同

时间:2013-11-04 19:40:16

标签: android colors calendar

我使用CALENDAR_COLOR列作为int得到颜色,但颜色与日历中显示的颜色不同。
事实上,它的颜色相似,但更轻!有什么理由发生?

由于

3 个答案:

答案 0 :(得分:4)

Google日历应用会对颜色进行转换。它使用硬编码的地图来搜索颜色对应。如果在地图上找不到颜色,则会应用手动转换。

这是代码的简化版(但功能性):

public final class CalendarUtils {

private static final Map<Integer, Integer> sUpdatedColors;

static {
    Map hashMap = new HashMap();
    sUpdatedColors = hashMap;
    hashMap.put(-509406, -2818048);
    sUpdatedColors.put(-370884, -765666);
    sUpdatedColors.put(-35529, -1086464);
    sUpdatedColors.put(-21178, -1010944);
    sUpdatedColors.put(-339611, -606426);
    sUpdatedColors.put(-267901, -1784767);
    sUpdatedColors.put(-4989844, -4142541);
    sUpdatedColors.put(-8662712, -8604862);
    sUpdatedColors.put(-15292571, -16023485);
    sUpdatedColors.put(-12396910, -16738680);
    sUpdatedColors.put(-7151168, -13388167);
    sUpdatedColors.put(-6299161, -16540699);
    sUpdatedColors.put(-6306073, -12417548);
    sUpdatedColors.put(-11958553, -12627531);
    sUpdatedColors.put(-6644481, -8812853);
    sUpdatedColors.put(-4613377, -5005861);
    sUpdatedColors.put(-5997854, -6395473);
    sUpdatedColors.put(-3312410, -7461718);
    sUpdatedColors.put(-3365204, -5434281);
    sUpdatedColors.put(-618062, -2614432);
    sUpdatedColors.put(-3118236, -1672077);
    sUpdatedColors.put(-5475746, -8825528);
    sUpdatedColors.put(-4013374, -10395295);
    sUpdatedColors.put(-3490369, -5792882);
    sUpdatedColors.put(-2350809, -2818048);
    sUpdatedColors.put(-18312, -765666);
    sUpdatedColors.put(-272549, -606426);
    sUpdatedColors.put(-11421879, -16023485);
    sUpdatedColors.put(-8722497, -13388167);
    sUpdatedColors.put(-12134693, -16540699);
    sUpdatedColors.put(-11238163, -12627531);
    sUpdatedColors.put(-5980676, -8812853);
    sUpdatedColors.put(-2380289, -7461718);
    sUpdatedColors.put(-30596, -1672077);
    sUpdatedColors.put(-1973791, -10395295);
    sUpdatedColors.put(-2883584, -2818048);
    sUpdatedColors.put(-831459, -765666);
    sUpdatedColors.put(-1152256, -1086464);
    sUpdatedColors.put(-1076736, -1010944);
    sUpdatedColors.put(-672219, -606426);
    sUpdatedColors.put(-1914036, -1784767);
    sUpdatedColors.put(-4208334, -4142541);
    sUpdatedColors.put(-8670655, -8604862);
    sUpdatedColors.put(-16089278, -16023485);
    sUpdatedColors.put(-16738937, -16738680);
    sUpdatedColors.put(-16606492, -16540699);
    sUpdatedColors.put(-12483341, -12417548);
    sUpdatedColors.put(-12624727, -12627531);
    sUpdatedColors.put(-8878646, -8812853);
    sUpdatedColors.put(-5071654, -5005861);
    sUpdatedColors.put(-7527511, -7461718);
    sUpdatedColors.put(-5500074, -5434281);
    sUpdatedColors.put(-2680225, -2614432);
    sUpdatedColors.put(-1737870, -1672077);
    sUpdatedColors.put(-8891321, -8825528);
    sUpdatedColors.put(-10263709, -10395295);
}

public static int getDisplayColor(int color) {
    if (sUpdatedColors.containsKey(color)) {
        return (sUpdatedColors.get(color));
    }
    if (sUpdatedColors.containsValue(color)) {
        return color;
    }
    float[] fArr = new float[3];
    Color.colorToHSV(color, fArr);
    if (fArr[2] > 0.79f) {
        fArr[1] = Math.min(fArr[1] * 1.3f, 1.0f);
        fArr[2] = fArr[2] * 0.8f;
    }
    return Color.HSVToColor(Color.alpha(color), fArr);
}

}

要获取转换后的颜色,可以使用Calendar Provider返回的颜色调用getDisplayColor(color)方法(因为Calendar Provider返回一个字符串,我们需要先将其解析为整数):

int calendarColor = Integer.parseInt(colorStringFromCalendarProvider);
int transformedColor = CalendarUtils.getDisplayColor(calendarColor);

答案 1 :(得分:0)

初始化变量

private View mView;

使用变量绑定视图

mView = (View) findViewById(R.id.mView);

日历内容提供商将返回颜色代码,您必须编写代码才能获取日历的颜色代码

String calendarColorCode = "-5997854";

转换为int color

int mColorCode = (0xff000000 + Integer.parseInt(calendarColorCode));

在视图上应用背景颜色代码

viewColorCode.setBackgroundColor(mColorCode);

完成!

答案 2 :(得分:0)

您也可以使用字符串,只要它是ARGB有效代码,例如“0xFF005500”

ARGB代表Alpha / Red / Green / Blue,知道你可以轻松地将#FF8866之类的HEX(RGB)代码转换为ARGB 0xFFFF8866。

在给出的示例中,红色代码为FF,绿色代码为88,蓝色代码为66.默认情况下,这将是不透明的颜色,因此将FF指定为alpha,导致ARGB代码为0xFFFF8866。 / p>