Uni*_*121 6 animation android kotlin
是否有人可以解释我使用"with"功能?
签名
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
Run Code Online (Sandbox Code Playgroud)
文件
以给定接收器作为接收器调用指定的函数f并返回其结果.
我发现它在这个项目中使用了Antonio Leiva.它用于移动视图:
fun View.animateTranslationY(translationY: Int, interpolator: Interpolator) {
with(ObjectAnimator.ofFloat(this, "translationY", translationY.toFloat())) {
setDuration(context.resources.getInteger(R.integer.config_mediumAnimTime).toLong())
setInterpolator(interpolator)
start()
}
}
Run Code Online (Sandbox Code Playgroud)
我在想,我知道将其转移到的意义
fun View.animateTranslationX(translationX: Int, interpolator: Interpolator) {
with(ObjectAnimator()) {
ofFloat(this, "translationX", translationX.toFloat())
setDuration(context.resources.getInteger(R.integer.config_mediumAnimTime).toLong())
setInterpolator(interpolator)
start()
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有编译......但我认为这ObjectAnimaton
是接收器,它会得到我将在{}
括号中调用的所有内容.任何人都可以解释真正的意义并提供一个基本的例子 - 至少比这更基本吗?:d
这个想法与 Pascal 中的关键字相同with
。
无论如何,这里有三个具有相同语义的示例:
with(x) {
bar()
foo()
}
Run Code Online (Sandbox Code Playgroud)
with(x) {
this.bar()
this.foo()
}
Run Code Online (Sandbox Code Playgroud)
x.bar()
x.foo()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
247 次 |
最近记录: |