使用Flow封装了一套api,提供了一种简单的方法来在创建视图动画。
view.run {
launch(this.alpha(ANIMATION_DURATION, reverse = true))
}
rxLifeScope.launch {
view.alpha(ANIMATION_DURATION, reverse = true).collect()
}
//记得在view销毁时取消, 不推荐
val job = GlobalScope.launch {
view.alpha(ANIMATION_DURATION, reverse = true).collect()
}
job.cancel()
view.run {
launch(this.translation(500f, 500f, ANIMATION_DURATION,
reverse = true))
}
view.run {
launch(this.scale(0f, ANIMATION_DURATION, reverse = true))
}
view.run {
launch(this.backgroundColor(
ContextCompat.getColor(this@MainActivity, R.color.primary),
ContextCompat.getColor(this@MainActivity, R.color.accent),
ANIMATION_DURATION, reverse = true
))
}
view.run {
launch(this.rotation(360f, ANIMATION_DURATION))
}
view.run {
launch(this.(0f to 30f).rangeAnyToCompletable(ANIMATION_DURATION) {
view.text = format.format(it as Float)
})
}
view.run {
launch(this.reveal(
viewX.toInt(),
viewY.toInt(),
0f,
hypot(
this.width.toDouble(),
this.height.toDouble()
).toFloat(),
2000L
))
}
AnimationX.together(
binding.fab.rotation(360f, ANIMATION_DURATION),
binding.text.fadeIn(ANIMATION_DURATION)
).collect()
AnimationX.sequentially(
binding.cardView.scale(1f, ANIMATION_DURATION),
binding.fab.scale(1f, ANIMATION_DURATION),
binding.progressBar.fadeIn(ANIMATION_DURATION)
).collect()
view.run {
launch(this.fadeOut().onCompletion {
//do something
})
}
view.run {
launch(this.fadeOut())
}
view.run {
launch(this.fadeIn())
}
view.run {
launch(this.shake())
}
view.run {
launch(this.press())
}
testview.run {
launch(this.text("Pateo", ANIMATION_DURATION, reverse = true))
}