Home > Blockchain >  Is there any APIs that applys the same thing into different views in Kotlin?
Is there any APIs that applys the same thing into different views in Kotlin?

Time:02-21

I have 3 buttons that has the same animation.

let's call them, button1, button2, button3.

button1
    .animate()
    .setDuration(initialTime)
    .setInterpolator(DecelerateInterpolator())
    .alpha(1f)
    .start()
button2
    .animate()
    .setDuration(initialTime)
    .setInterpolator(DecelerateInterpolator())
    .alpha(1f)
    .start()
button3
    .animate()
    .setDuration(initialTime)
    .setInterpolator(DecelerateInterpolator())
    .alpha(1f)
    .start()

But this is too long. I need something like...

with(button1 button2 button3).apply{
    this.animate()
    .setDuration(initialTime)
    .setInterpolator(DecelerateInterpolator())
    .alpha(1f)
    .start()
}

This will be clearer and simpler. I could make a list of the views but this method is also longer and massier.

Is there any features like that in Kotlin?

CodePudding user response:

val buttons = listOf(button1, button2, button3)
buttons.forEach { 
    it.animate()
    .setDuration(initialTime)
    .setInterpolator(DecelerateInterpolator())
    .alpha(1f)
    .start()
}

CodePudding user response:

extension function

fun Button.setUp() {
    this.animate()
        .setDuration(initialTime)
        .setInterpolator(DecelerateInterpolator())
        .alpha(1f)
        .start()
}
button1.setUp()
button2.setUp()
button3.setUp()
  • Related