18.Android 颜色工具类ColorUtil

18.Android 颜色工具类ColorUtil

public class ColorUtil {

    /**
     * 根据颜色资源Id,取得颜色
     *
     * @param colorId
     * @return color
     */
    public static int getResourcesColor(Context context, int colorId) {
        int color = 0x00ffffff;
        try {
            color = context.getResources().getColor(colorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return color;
    }

    /**
     * 将十六进制 颜色代码 转换为 int
     *
     * @return color
     */
    public static int HextoColor(String color) {
        // #00000000 - #ffffffff
        String reg = "#[a-f0-9A-F]{8}";
        if (!Pattern.matches(reg, color)) {
            color = "#ffffffff";
        }
        return Color.parseColor(color);
    }

    /**
     * 设置颜色透明度
     *
     * @param color
     * @param alpha
     * @return color
     */
    public static int setColorAlpha(int color, int alpha) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        return Color.argb(alpha, red, green, blue);
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ColorUtil是一个用于处理颜色工具类,它提供了一些常见的颜色操作方法,包括颜色转换、颜色混合、颜色亮度调整等等。 以下是ColorUtil工具类的示例代码: ```java public class ColorUtil { /** * 将RGB颜色值转换为16进制表示的字符串 * * @param color RGB颜色值 * @return 16进制表示的字符串 */ public static String toHex(int color) { String hex = Integer.toHexString(color); if (hex.length() == 8) { hex = hex.substring(2); } return "#" + hex.toUpperCase(Locale.getDefault()); } /** * 将16进制表示的颜色值转换为RGB颜色值 * * @param hex 16进制表示的颜色值 * @return RGB颜色值 */ public static int toRgb(String hex) { if (hex.startsWith("#")) { hex = hex.substring(1); } if (hex.length() == 3) { hex = hex.substring(0, 1) + hex.substring(0, 1) + hex.substring(1, 2) + hex.substring(1, 2) + hex.substring(2, 3) + hex.substring(2, 3); } if (hex.length() != 6) { throw new IllegalArgumentException("Invalid hex color: " + hex); } return Color.parseColor("#" + hex.toUpperCase(Locale.getDefault())); } /** * 将两种颜色按照指定的比例混合 * * @param color1 第一种颜色 * @param color2 第二种颜色 * @param ratio 混合比例,取值范围[0, 1] * @return 混合后的颜色 */ public static int blend(int color1, int color2, float ratio) { float r = (Color.red(color1) * ratio) + (Color.red(color2) * (1 - ratio)); float g = (Color.green(color1) * ratio) + (Color.green(color2) * (1 - ratio)); float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * (1 - ratio)); return Color.rgb((int) r, (int) g, (int) b); } /** * 调整颜色的亮度 * * @param color 要调整的颜色 * @param factor 亮度调整因子,取值范围[-1, 1] * @return 调整后的颜色 */ public static int adjustBrightness(int color, float factor) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[2] *= (1 + factor); return Color.HSVToColor(hsv); } } ``` ColorUtil工具类包含了以下方法: - **toHex(int color)**:将RGB颜色值转换为16进制表示的字符串。 - **toRgb(String hex)**:将16进制表示的颜色值转换为RGB颜色值。 - **blend(int color1, int color2, float ratio)**:将两种颜色按照指定的比例混合。 - **adjustBrightness(int color, float factor)**:调整颜色的亮度。 这些方法都是静态方法,可以直接通过类名调用。要使用ColorUtil工具类,只需要将它的代码复制到自己的项目中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值