Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Build list programatically in Android Kotlin

Any ideas how to create this list dinamically using some math functions or something? It looks really ugly like this. There isn’t an exact pattern for how the colors are so I’m not sure how to do this. Help would be much appreciated.

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
)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Given that there is no real pattern, I would do it like this to make it more compact:

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) }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading