java 16进制转rgba方法
时间: 2024-02-05 08:04:28 浏览: 30
以下是 Java 中将 16 进制颜色代码转换为 RGBA 值的示例代码:
```java
public static int[] hexToRgba(String hex) {
int[] rgba = new int[4];
String hexCode = hex.substring(1); // 去掉 # 号
if (hexCode.length() == 6) {
rgba[0] = Integer.parseInt(hexCode.substring(0, 2), 16);
rgba[1] = Integer.parseInt(hexCode.substring(2, 4), 16);
rgba[2] = Integer.parseInt(hexCode.substring(4, 6), 16);
rgba[3] = 255; // 默认 alpha 值为 255
} else if (hexCode.length() == 8) {
rgba[0] = Integer.parseInt(hexCode.substring(0, 2), 16);
rgba[1] = Integer.parseInt(hexCode.substring(2, 4), 16);
rgba[2] = Integer.parseInt(hexCode.substring(4, 6), 16);
rgba[3] = Integer.parseInt(hexCode.substring(6, 8), 16);
}
return rgba;
}
```
此方法将参数 hex 中的 16 进制颜色代码转换为一个包含 RGBA 值的整型数组,其中 RGBA 值的范围都为 0~255。例如,将参数 "#FF0000FF" 传递给此方法将返回一个长度为 4 的整型数组,它的元素依次为 {255, 0, 0, 255},表示红色。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)