j2me绘制渐变颜色

改进了网上一个j2me绘制渐变颜色的方法

 

/**
     * 绘制渐变颜色
     * @param gr 画笔
     * @param startcolor 起始色值
     * @param endcolor   终点色值
     * @param direction  渐变方向 0-水平; 1-垂直
     * @param x 绘制区域x
     * @param y 绘制区域y
     * @param w 绘制区域w
     * @param h 绘制区域h
     */
    public static void drawGradientColor(Graphics gr, int startcolor, int endcolor, int direction, int x, int y, int w, int h) {
        int r_start = ((startcolor >>> 16) & 0xFF);
        int g_start = ((startcolor >>> 8) & 0xFF);
        int b_start = ((startcolor >>> 0) & 0xFF);

 

        int r_end = ((endcolor >>> 16) & 0xFF);
        int g_end = ((endcolor >>> 8) & 0xFF);
        int b_end = ((endcolor >>> 0) & 0xFF);

 

        int c_r = r_end - r_start;
        int c_g = g_end - g_start;
        int c_b = b_end - b_start;

 

        if(direction == 0) {
            for (int i = 0; i < w; i++) {
                int r = r_start + c_r * i / w;
                int g = g_start + c_g * i / w;
                int b = b_start + c_b * i / w;

                gr.setColor(r, g, b);
                gr.fillRect(x + i, y, 1, h);
            }
        } else if(direction == 1) {
            for (int i = 0; i < h; i++) {
                int r = r_start + c_r * i / h;
                int g = g_start + c_g * i / h;
                int b = b_start + c_b * i / h;

                gr.setColor(r, g, b);
                gr.fillRect(x, y + i, w, 1);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值