ObjectAnimator 类型的解构声明初始值设定项
Destructuring declaration initializer of type ObjectAnimator
我正在使用 kotlin 解构声明。我之前将它与 SpringAnimation
一起使用,并且效果很好。现在我想将它与 ObjectAnimator
一起使用,但出现此错误:
Destructuring declaration initializer of type ObjectAnimator! must have a 'component1()' function
Destructuring declaration initializer of type ObjectAnimator! must have a 'component2()' function
这是我的代码:
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
}
}
怎么了?
这里的问题是您不能在新行上开始中缀调用函数调用 - 编译器基本上会推断出 semicolon/line 在您的第一个 apply
调用之后结束。这与运算符的方式相同,例如 see this issue。
所以您需要重新格式化您的代码以便 to
连接,最简单的是这样的:
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
}
)
}
或介于两者之间。
我正在使用 kotlin 解构声明。我之前将它与 SpringAnimation
一起使用,并且效果很好。现在我想将它与 ObjectAnimator
一起使用,但出现此错误:
Destructuring declaration initializer of type ObjectAnimator! must have a 'component1()' function
Destructuring declaration initializer of type ObjectAnimator! must have a 'component2()' function
这是我的代码:
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
}
}
怎么了?
这里的问题是您不能在新行上开始中缀调用函数调用 - 编译器基本上会推断出 semicolon/line 在您的第一个 apply
调用之后结束。这与运算符的方式相同,例如 see this issue。
所以您需要重新格式化您的代码以便 to
连接,最简单的是这样的:
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
}
)
}
或介于两者之间。