Kotlin 扩展库KTX

首先给出文档地址:

Doc:https://android.github.io/android-ktx/core-ktx/

Github:https://github.com/android/android-ktx

 

添加依赖:


 //Kotlin依赖
 implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
 implementation 'androidx.core:core-ktx:1.2.0'

 //对KTX的支持 livedata、viewmodel
 implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
 implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
 implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

Kotlin KTX 是什么?

是一个Kotlin 的扩展库,它能使本来已经简洁的Kotlin代码更加简洁,提高开发者开发者效率和使用体验

简单对比

更多细节,请参考官方文档

Kotlin


        view = findViewById(R.id.view)
        val animator = ObjectAnimator.ofFloat(view, "scaleY", 6f, 1f)
        animator.addListener(object :Animator.AnimatorListener{
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }

        })

Kotlin with KTX

        view = findViewById(R.id.view)
        val animator = ObjectAnimator.ofFloat(view, "scaleY", 6f, 1f)
        animator.addListener {

        }

 

你可能感兴趣的:(Kotlin实战)