欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-05-21 08:30:00|栏目:Android代码|点击:

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);
}

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

上一篇:Android图片缓存原理、特性对比

栏    目:Android代码

下一篇:Android使用Sqlite存储数据用法示例

本文标题:Android Color颜色过度计算实现代码

本文地址:http://www.codeinn.net/misctech/126212.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有