Paint.setColor
需要一个整数作为参数。但我有一个Color
对象。在Java中没有color.getIntValue()
?那我该怎么办呢?我想要的是像这样的东西:
public Something myMethod(Color rgb){
myPaint.setColor(rgb.getIntValue());
...
}
更正: android.graphics.Color;
我以为把android
作为标签之一就足够了。但显然不是这样。
Paint.setColor
需要一个整数作为参数。但我有一个Color
对象。在Java中没有color.getIntValue()
?那我该怎么办呢?我想要的是像这样的东西:
public Something myMethod(Color rgb){
myPaint.setColor(rgb.getIntValue());
...
}
更正: android.graphics.Color;
我以为把android
作为标签之一就足够了。但显然不是这样。
首先,android.graphics.Color是一个仅由静态方法组成的类。那么您是如何创建新的android.graphics.Color对象的?(这完全没有意义,对象本身不存储任何数据)
但无论如何...我假设您正在使用实际存储数据的某个对象...
一个整数由4个字节组成(在Java中)。查看标准Java Color对象的getRGB()函数,我们可以看到Java按照ARGB(Alpha-Red-Green-Blue)的顺序将每种颜色映射到整数的一个字节。我们可以通过以下自定义方法复制此行为:
public int getIntFromColor(int Red, int Green, int Blue){
Red = (Red << 16) & 0x00FF0000; //Shift red 16-bits and mask out other stuff
Green = (Green << 8) & 0x0000FF00; //Shift Green 8-bits and mask out other stuff
Blue = Blue & 0x000000FF; //Mask out anything not blue.
return 0xFF000000 | Red | Green | Blue; //0xFF000000 for 100% Alpha. Bitwise OR everything together.
}
假设您可以以某种方式检索到单独的红色、绿色和蓝色颜色分量,并且您传递给颜色的所有值都在0-255之间。
如果您的RGB值是介于0和1之间的浮点百分比形式,请考虑以下方法:
public int getIntFromColor(float Red, float Green, float Blue){
int R = Math.round(255 * Red);
int G = Math.round(255 * Green);
int B = Math.round(255 * Blue);
R = (R << 16) & 0x00FF0000;
G = (G << 8) & 0x0000FF00;
B = B & 0x000000FF;
return 0xFF000000 | R | G | B;
}
正如其他人所说,如果你使用标准的Java对象,只需使用getRGB();
如果您决定正确使用Android颜色类,也可以执行以下操作:
int RGB = android.graphics.Color.argb(255, Red, Green, Blue); //Where Red, Green, Blue are the RGB components. The number 255 is for 100% Alpha
或者int RGB = android.graphics.Color.rgb(Red, Green, Blue); //Where Red, Green, Blue are the RGB components.
就像其他人所说的那样...(第二个函数假设 alpha 值为 100%)
这两种方法基本上与上面创建的第一种方法执行相同的操作。
myPaint.setColor(Color.rgb(int, int, int));
要检索单个颜色值,您可以使用相应的方法:
Color.red(int color)
Color.blue(int color)
Color.green(int color)
请参考此文档获取更多信息
Color
类有一个getRGB()方法,该方法返回颜色值的 int
表示。
android.graphics.Color;
。 - Cote Mounyointvalue = Color.parseColor("#" + colorobject);
。这个代码与颜色相关,可以将颜色值转换为整数值。int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
context.getColor(int resId);
int color = Color.rgb(red, green, blue);
其中红色、绿色和蓝色是介于0到255之间的整数值。
使用getRGB(),它可以帮助你(无需复杂的程序)
从图像数据的一部分返回默认RGB颜色模型(TYPE_INT_ARGB)和默认sRGB颜色空间中的整数像素数组。
Color color = new Color (10,10,10)
myPaint.setColor(color.getRGB());