您的位置:首页 > 其它

j2me中生成渐变的颜色条

2011-01-26 11:45 239 查看
/**
* 生成彩色条
* @param startColor
* @param endColor
* @param w
* @param h
* @param step
* @return
*/
public static Image createBarImage(int startColor, int endColor, int w, int h, int step)
{
Image colorBarImage = Image.createImage(w, h);
Graphics g = colorBarImage.getGraphics();
drawTransition(g, 0, w, startColor, endColor, step, h, 0, false);

return colorBarImage;
}

/**
* 画渐变的bar
* @param g
* @param y
* @param total
* @param startColor
* @param endColor
* @param perStep
* @param len
* @param x
* @param isHor
*/
public static void drawTransition(Graphics g, int y, int total, int startColor, int endColor, int perStep, int len, int x, boolean isHor)
{
int paintCount =0;
paintCount=(total + perStep - 1) / perStep;

for (int i = 0; i < paintCount; i++)
{
int color = getTransationColor(startColor, endColor, paintCount, i);
g.setColor(color);
if(isHor)
{
g.fillRect(x, y, len, perStep);
y += perStep;
}
else
{
g.fillRect(x, y, perStep, len);
x+=perStep;
}
}
}

/**
* 从开始颜色渐变到结束颜色
* @param startColor
* @param endColor
* @param totalCount
* @param currentCount
* @return
*/
public static int getTransationColor(int startColor, int endColor, int totalCount, int currentCount)
{
int sR = (startColor & 0xff0000) >> 16;
int sG = (startColor & 0x00ff00) >> 8;
int sB = (startColor & 0x0000ff) >> 0;
int eR = (endColor & 0xff0000) >> 16;
int eG = (endColor & 0x00ff00) >> 8;
int eB = (endColor & 0x0000ff) >> 0;
int tr = sR + (eR - sR) * currentCount / totalCount;
int tg = sG + (eG - sG) * currentCount / totalCount;
int tb = sB + (eB - sB) * currentCount / totalCount;
int color=(tr<<16)+(tg<<8)+tb;
return color;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: