android color between two colors, based on percentage?

后端 未结 9 1820
悲哀的现实
悲哀的现实 2020-11-29 23:17

I would like to calculate the color depending on a percentage value:

float percentage = x/total;
int color;
if (percentage >= 0.95) {
  color = Color.GREE         


        
9条回答
  •  一向
    一向 (楼主)
    2020-11-29 23:36

    As an updated solution, you can use ColorUtils#blendARGB from the Android support or AndroidX APIs:

    val startColor = ContextCompat.getColor(context, R.color.white)
    val endColor = ContextCompat.getColor(context, R.color.yellow)
    ColorUtils.blendARGB(startColor, endColor, 0.75)
    

提交回复
热议问题