I was addicted to it when I was playing with UI design in Android development, so I tried to organize it. You can specify the color from hexadecimal or from the resource file. There is no such thing as a resource file on iOS, so it took me a while to understand the concept. The tricky thing for me was the concept of a Resource file.
A method that can be used if the key is set in color.xml of the Res (Resource) folder. A common method in practice.
Resource.java
Resources res = getResources();
int chocolate_color = res.getColor(R.color.chocolate);
TextView text = new TextView(this);
text.setBackgroundColor(chocolate_color);
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="chocolate">#d2691e</color>
</resources>
Rgb.java
TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("#FF00C0"));
// "#FF_00_C0"
// RGB
The parseColor method is
public static int parseColor(String colorString)
So the argument is of type String
and the return value is of type ʻint. Color seems to be ʻint color
.
Argb.java
TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("#FF0F00C0"));
// "#FF_0F_00_C0"
// ARGB
Keywords that can be used are the keywords below
red
blue
green
black
white
gray
cyan
magenta
yellow
lightgraydarkgray
Word.java
TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("magenta"));
--Color.BLACK
: Black
--Color.BLUE
: Blue
--Color.CYAN
: Cyan
--Color.DKGRAY
: Dark gray
--Color.GRAY
: Gray
--Color.GREEN
: Green
--Color.LTGRAY
: Light gray
--Color.MAGENTA
: Magenta
--Color.RED
: Red
--Color.TRANSPARENT
: Transparent
--Color.WHITE
: White
--Color.YELLOW
: Yellow
Intcolor.java
String white = "#ffffff";
int whiteInt = Color.parseColor(white);
TextView text = new TextView(this);
text.setBackgroundColor(whiteInt);
hex.java
public static String bin2hex(byte[] data) {
StringBuffer sb = new StringBuffer();
for (byte b : data) {
String s = Integer.toHexString(0xff & b);
if (s.length() == 1) {
sb.append("0");
}
sb.append(s);
}
return sb.toString();
}
public static byte[] hex2bin(String hex) {
byte[] bytes = new byte[hex.length() / 2];
for (int index = 0; index < bytes.length; index++) {
bytes[index] = (byte) Integer.parseInt(hex.substring(index * 2, (index + 1) * 2), 16);
}
return bytes;
}
I think that if you hold down this much, you will be able to perform color conversion immediately after seeing this page.
Recommended Posts