欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-10 20:21:52
android color颜色过度计算实现代码 在看自定义typeevaluator来计算属性动画的属性值时,用到了对颜色的过度计算,翻看了好多博客,找到了比较有优秀的解...

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

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

实现效果图:

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

实现代码:

/**
 * 根据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);
}

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