实现 颜色渐变

private static final int TextShade = 0x0000BEff; //渐变开始时的颜色
        private static final int TransEnd = 0x00ffAD0C; //渐变结束时的颜色
        /**********************************
        *
        * 输入一个颜色,将它拆成三个部分:
        * 红色,绿色和蓝色
        *
        **********************************/
        public static int[] retrieveRGBComponent(int color) {
        int[] rgb = new int[3];
        rgb[0] = (color & 0x00ff0000) >> 16;
        rgb[1] = (color & 0x0000ff00) >> 8;
        rgb[2] = (color & 0x000000ff);
        return rgb;
        }
        /**********************************
        *
        * color1是浅色,color2是深色,实现渐变
        * steps是指在多大的区域中渐变,
        * 在左右渐变是steps就是宽
        * 在上下渐变时steps就是高
        *
        **********************************/
        public static int[] generateTransitionalColor(int color1, int color2, int steps) {
        int[] color1RGB = retrieveRGBComponent(color1);
        int[] color2RGB = retrieveRGBComponent(color2);
        if (steps < 3 || color1RGB == null || color2RGB == null)
        return null;
        int[] colors = new int[steps];
        colors[0] = color1;
        colors[colors.length - 1] = color2;
        steps = steps - 2;
        int redDiff = color2RGB[0] - color1RGB[0];
        int greenDiff = color2RGB[1] - color1RGB[1];
        int blueDiff = color2RGB[2] - color1RGB[2];
//        from the second to the last second.
        for (int i = 1; i < colors.length - 1; i++) {
        colors[i] = generateFromRGBComponent(new int[] { color1RGB[0] + redDiff * i / steps,
        color1RGB[1] + greenDiff * i / steps, color1RGB[2] + blueDiff * i / steps });
        }
        return colors;
        }
        /**********************************
        *
        *
        * 红色,绿色和蓝色三色组合
        *
        **********************************/
        public static int generateFromRGBComponent(int[] rgb) {
        if (rgb == null || rgb.length != 3 || rgb[0] < 0 || rgb[0] > 255 || rgb[1] < 0
        || rgb[1] > 255 || rgb[2] < 0 || rgb[2] > 255)
        return 0xfffff;
        return rgb[0] << 16 | rgb[1] << 8 | rgb[2];
        }
        /**********************************
        *
        * 按照上面的方法生成的窄条绘图
        *
        **********************************/
        public static void drawSelectedBackground(Graphics g, int x, int y, int width, int height) {
//         这样写是从左到右渐变

        int[] line = generateTransitionalColor(TextShade, TransEnd, width);
        Image lineImg = Image.createRGBImage(line, line.length, 1, false);
        for (int i = 0; i < height; i++) {
        g.drawImage(lineImg, x, y + i, Graphics.LEFT|Graphics.TOP);
        }

//         下面的写法是从上到下渐变

//        int[] line = generateTransitionalColor(TextShade, TransEnd, height);
//         Image lineImg = Image.createRGBImage(line, 1, line.length, false);
//         for (int i = 0; i < width; i++) {
//         g.drawImage(lineImg, x+i, y, Graphics.LEFT|Graphics.TOP);
//         }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值