当前位置: 移动技术网 > IT编程>移动开发>Android > Android Color颜色过度计算实现代码

Android Color颜色过度计算实现代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

逢魔林怎么去,我的答铃英文版,安徽省太和县

android color颜色过度计算实现代码

在看自定义typeevaluator来计算属性动画的属性值时,用到了对颜色的过度计算,翻看了好多博客,找到了比较有优秀的解决方案,在此记录,以备后用。

实现效果图:

实现代码:

/**
 * 根据fraction值来计算当前的颜色。
 */
private int getcurrentcolor(float fraction, int startcolor, int endcolor) {
  int redcurrent;
  int bluecurrent;
  int greencurrent;
  int alphacurrent;

  int redstart = color.red(startcolor);
  int bluestart = color.blue(startcolor);
  int greenstart = color.green(startcolor);
  int alphastart = color.alpha(startcolor);

  int redend = color.red(endcolor);
  int blueend = color.blue(endcolor);
  int greenend = color.green(endcolor);
  int alphaend = color.alpha(endcolor);

  int reddifference = redend - redstart;
  int bluedifference = blueend - bluestart;
  int greendifference = greenend - greenstart;
  int alphadifference = alphaend - alphastart;

  redcurrent = (int) (redstart + fraction * reddifference);
  bluecurrent = (int) (bluestart + fraction * bluedifference);
  greencurrent = (int) (greenstart + fraction * greendifference);
  alphacurrent = (int) (alphastart + fraction * alphadifference);

  return color.argb(alphacurrent, redcurrent, greencurrent, bluecurrent);
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网