j2me中生成渐变的颜色条

/**
	 * 生成彩色条
	 * @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;
    }

posted on 2011-01-26 11:45  台哥编程课堂  阅读(289)  评论(0编辑  收藏  举报

导航