如何将返回的int值从ColorDrawable的getColor()转换为RGB?

时间:2014-07-28 21:25:19

标签: java android colors android-studio

所以我有以下代码片段来检索活动的背景颜色。但是,getColor返回一个int值,并且似乎没有办法修改它以返回更标准的格式来处理和修改。

    setContentView(R.layout.activity_test);

    View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);

    int color = Color.TRANSPARENT;
    Drawable background = root.getBackground();
    if (background instanceof ColorDrawable)
        color = ((ColorDrawable) background).getColor();

在这种特殊情况下,活动的背景颜色最初在XML文件中定义为android:background="#0099cc",getColor()返回为-16737844。但是,我想通过逐步改变颜色的rgb值来改变它[即RGB(initialRVal + 1,initialGVal + 1,initialBVal + 1)]。这需要我以某种方式将-16737844转换为一组RGB值,但似乎并没有这样做。

1 个答案:

答案 0 :(得分:4)

您想将十六进制颜色值转换为RGB。

int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = (color >> 0) & 0xFF;