您的位置:首页 > 其它

j2me绘制渐变颜色

2010-08-21 03:01 330 查看
改进了网上一个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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: