Android ObjectAnimator类型的分解声明初始值设定项

Android ObjectAnimator类型的分解声明初始值设定项,android,kotlin,objectanimator,Android,Kotlin,Objectanimator,我正在使用kotlin解构声明。我以前在SpringAnimation中使用过它,效果非常好。现在我想将它与ObjectAnimator一起使用,我得到了以下错误: 正在分解ObjectAnimator类型的声明初始值设定项!必须具有“component1()”函数 正在分解ObjectAnimator类型的声明初始值设定项!必须具有“component2()”函数 这是我的密码: val (xanimator, alphaanim) = findViewById<View>(R.i

我正在使用kotlin解构声明。我以前在
SpringAnimation
中使用过它,效果非常好。现在我想将它与
ObjectAnimator
一起使用,我得到了以下错误:

正在分解ObjectAnimator类型的声明初始值设定项!必须具有“component1()”函数

正在分解ObjectAnimator类型的声明初始值设定项!必须具有“component2()”函数

这是我的密码:

val (xanimator, alphaanim) = findViewById<View>(R.id.imageView).let { img ->
            ObjectAnimator.ofFloat(img, "translationX", 100f).apply {
                duration = 2000
            }
            to
            ObjectAnimator.ofFloat(img, "alpha", 1.0f).apply {
                duration = 2000
            }
        }
val(xanimator,alphaanim)=findviewbyd(R.id.imageView)。让{img->
卸载(img,“translationX”,100f)。应用{
持续时间=2000年
}
到
卸载(img,“alpha”,1.0f)。应用{
持续时间=2000年
}
}

怎么了?

这里的问题是不能在新行上启动中缀调用函数调用-编译器基本上推断出在第一次
apply
调用后结束的分号/行。例如,对于运算符也是如此

因此,您需要将代码重新格式化一点,以便
连接到
,最简单的方式如下:

val (xanimator: ObjectAnimator, alphaanim: ObjectAnimator) = findViewById<View>(R.id.imageView).let { img ->
    ObjectAnimator.ofFloat(img, "translationX", 100f).apply {
        duration = 2000
    } to
    ObjectAnimator.ofFloat(img, "alpha", 1.0f).apply {
        duration = 2000
    }
}
val (xanimator: ObjectAnimator, alphaanim: ObjectAnimator) = findViewById<View>(R.id.imageView).let { img ->
    Pair(
            ObjectAnimator.ofFloat(img, "translationX", 100f).apply {
                duration = 2000
            },
            ObjectAnimator.ofFloat(img, "alpha", 1.0f).apply {
                duration = 2000
            }
    )
}
val(xanimator:ObjectAnimator,alphaanim:ObjectAnimator)=findviewbyd(R.id.imageView)。让{img->
卸载(img,“translationX”,100f)。应用{
持续时间=2000年
}到
卸载(img,“alpha”,1.0f)。应用{
持续时间=2000年
}
}
但是为了可读性,也许你可以这样做:

val (xanimator: ObjectAnimator, alphaanim: ObjectAnimator) = findViewById<View>(R.id.imageView).let { img ->
    ObjectAnimator.ofFloat(img, "translationX", 100f).apply {
        duration = 2000
    } to
    ObjectAnimator.ofFloat(img, "alpha", 1.0f).apply {
        duration = 2000
    }
}
val (xanimator: ObjectAnimator, alphaanim: ObjectAnimator) = findViewById<View>(R.id.imageView).let { img ->
    Pair(
            ObjectAnimator.ofFloat(img, "translationX", 100f).apply {
                duration = 2000
            },
            ObjectAnimator.ofFloat(img, "alpha", 1.0f).apply {
                duration = 2000
            }
    )
}
val(xanimator:ObjectAnimator,alphaanim:ObjectAnimator)=findviewbyd(R.id.imageView)。让{img->
配对(
卸载(img,“translationX”,100f)。应用{
持续时间=2000年
},
卸载(img,“alpha”,1.0f)。应用{
持续时间=2000年
}
)
}
或两者之间的任何内容。

请检查一下: