颜色渐变实用获取色值方法

时间:2022-11-26 11:25:09
/**
	 * 计算渐变颜色值 ARGB
	 * 
	 * @param fraction
	 *            变化比率 0~1
	 * @param startValue
	 *            初始色值
	 * @param endValue
	 *            结束色值
	 * @return
	 */
	private Integer evaluate(float fraction, Object startValue, Integer endValue) {
		int startInt = (Integer) startValue;
		int startA = (startInt >> 24) & 0xff;
		int startR = (startInt >> 16) & 0xff;
		int startG = (startInt >> 8) & 0xff;
		int startB = startInt & 0xff;
		int endInt = (Integer) endValue;
		int endA = (endInt >> 24) & 0xff;
		int endR = (endInt >> 16) & 0xff;
		int endG = (endInt >> 8) & 0xff;
		int endB = endInt & 0xff;
		return (int) ((startA + (int) (fraction * (endA - startA))) << 24)
				| (int) ((startR + (int) (fraction * (endR - startR))) << 16)
				| (int) ((startG + (int) (fraction * (endG - startG))) << 8)
				| (int) ((startB + (int) (fraction * (endB - startB))));
	}


使用方法很简单:

int color = evaluate(0.5f, Color.BLACK, Color.WHITE);