在 Android Kotlin 中以编程方式构建列表

Build list programatically in Android Kotlin

关于如何使用一些数学函数或其他东西动态地创建这个列表有什么想法吗?这样子真的很难看。没有关于颜色如何的确切模式,所以我不确定如何做到这一点。将不胜感激。

val colorGradient = listOf(
    backgroundColor.copy(alpha = 1f),
    backgroundColor.copy(alpha = 0.95f),
    backgroundColor.copy(alpha = 0.95f),
    backgroundColor.copy(alpha = 0.95f),
    backgroundColor.copy(alpha = 0.9f),
    backgroundColor.copy(alpha = 0.9f),
    backgroundColor.copy(alpha = 0.9f),
    backgroundColor.copy(alpha = 0.85f),
    backgroundColor.copy(alpha = 0.85f),
    backgroundColor.copy(alpha = 0.85f),
    backgroundColor.copy(alpha = 0.85f),
    backgroundColor.copy(alpha = 0.8f),
    backgroundColor.copy(alpha = 0.8f),
    backgroundColor.copy(alpha = 0.7f),
    backgroundColor.copy(alpha = 0.7f),
    backgroundColor.copy(alpha = 0.6f),
    backgroundColor.copy(alpha = 0.55f),
    backgroundColor.copy(alpha = 0.5f),
    backgroundColor.copy(alpha = 0.5f),
    backgroundColor.copy(alpha = 0.4f),
    backgroundColor.copy(alpha = 0.3f),
    backgroundColor.copy(alpha = 0.2f),
    backgroundColor.copy(alpha = 0.1f),
    backgroundColor.copy(alpha = 0.05f),
    Color.Transparent,
    Color.Transparent
)

鉴于没有真正的模式,我会这样做以使其更紧凑:

val gradients = listOf(1.0f, 0.95f, 0.95f, 0.95f, 0.9f, 0.9f, 0.9f, 0.85f, 0.85f, 0.85f, 0.85f, 0.8f, 0.8f, 0.7f, 0.7f, 0.6f, 0.55f, 0.5f, 0.5f, 0.4f, 0.3f, 0.2f, 0.1f, 0.0f, 0.0f)
val colorGradient = gradients.map { backgroundColor.copy(alpha = it) }

您也可以从每个梯度步骤的计数列表开始:

val colorGradient = listOf(1, 3, 3, 4, 2, 0, 2, 0, 1, 1, 2, 0, 1, 0, 1, 0, 1, 0, 1, 1, 2)
  .flatMapIndexed { index, count -> List(count) { (1 - (index * 5) / 100.0).toFloat() } }