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

来源:互联网 发布:淘宝上的机油能买吗 编辑:程序博客网 时间:2024/06/05 00:32

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

/** * 计算从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 0
原创粉丝点击