【问题标题】:How to convert returned int value from ColorDrawable's getColor() to RGB?如何将返回的 int 值从 ColorDrawable 的 getColor() 转换为 RGB?
【发布时间】:2014-09-20 04:39:35
【问题描述】:

所以我有以下 sn-p 代码来检索活动的背景颜色。但是,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 值,但似乎没有办法这样做。

【问题讨论】:

    标签: java android colors android-studio


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多