Android Color 颜色过度计算实现方法

7 篇文章 0 订阅
5 篇文章 0 订阅

在项目中经常要做一些动画效果,一般会伴随颜色的变化,为了友好的交互,这个变化过程最好是平滑过度,这里是贴上一种计算两个颜色之间的色差在某个百分比情况的颜色值:

/**
	 * 计算从startColor过度到endColor过程中百分比为franch时的颜色值
	 * @param startColor 起始颜色 int类型
	 * @param endColor 结束颜色 int类型
	 * @param franch franch 百分比0.5
	 * @return 返回int格式的color
	 */
	public static int caculateColor(int startColor, int endColor, float franch){
		String strStartColor = "#" + Integer.toHexString(startColor);
		String strEndColor = "#" + Integer.toHexString(endColor);
		return Color.parseColor(caculateColor(strStartColor, strEndColor, franch));
	}
	
	/**
	 * 计算从startColor过度到endColor过程中百分比为franch时的颜色值
	 * @param startColor 起始颜色 (格式#FFFFFFFF)
	 * @param endColor 结束颜色 (格式#FFFFFFFF)
	 * @param franch 百分比0.5
	 * @return 返回String格式的color(格式#FFFFFFFF)
	 */
	public static String caculateColor(String startColor, String endColor, float franch){
		
		int startAlpha = Integer.parseInt(startColor.substring(1, 3), 16);
		int startRed = Integer.parseInt(startColor.substring(3, 5), 16);
		int startGreen = Integer.parseInt(startColor.substring(5, 7), 16);
		int startBlue = Integer.parseInt(startColor.substring(7), 16);
		
		int endAlpha = Integer.parseInt(endColor.substring(1, 3), 16);
		int endRed = Integer.parseInt(endColor.substring(3, 5), 16);
		int endGreen = Integer.parseInt(endColor.substring(5, 7), 16);
		int endBlue = Integer.parseInt(endColor.substring(7), 16);
		
		int currentAlpha = (int) ((endAlpha - startAlpha) * franch + startAlpha);
		int currentRed = (int) ((endRed - startRed) * franch + startRed);
		int currentGreen = (int) ((endGreen - startGreen) * franch + startGreen);
		int currentBlue = (int) ((endBlue - startBlue) * franch + startBlue);
		
		return "#" + getHexString(currentAlpha) + getHexString(currentRed)
				+ getHexString(currentGreen) + getHexString(currentBlue);
		
	}
	
	/** 
     * 将10进制颜色值转换成16进制。 
     */  
	public static String getHexString(int value) {  
        String hexString = Integer.toHexString(value);  
        if (hexString.length() == 1) {  
            hexString = "0" + hexString;  
        }  
        return hexString;  
    }  


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值