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;
    }

转载于:https://www.cnblogs.com/chaohi/archive/2011/01/26/1945316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值