Skip to content
Advertisement

How to interpret and translate a Lottie animation from Kotlin to Java?

I have been trying to translate this Kotlin code to Java since the project is in Java. I am translating by looking into Kotlin syntax. However, there are still others that I am having a hard time understanding.

https://github.com/airbnb/lottie-android/blob/master/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/AppIntroActivity.kt

Specifically:

JavaScript

For the setViewPagerScroller, I was able to translate the first part.

JavaScript

Advertisement

Answer

The method setViewPagerScroller uses kotlin anonymous inner class syntax. That is the ‘object’ part which has no real counterpart in java syntax.

JavaScript

And the as keyword is like a casting in java. Hopefully you can use this to translate fun generateFinalButtonBehaviour(), it contains more of the same.

The Lazy construct unsurprisingly gets more verbose in java. You have to employ discipline to not access the viewpager wrongly if you choose to follow the structure below.

JavaScript

You can also use a class to more accurately represent a lazy initialization of your fields. (So that you cannot by mistake access a field which you meant to be lazily initialized) This gets even more verbose but could be worth it.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement