首先是要操作的布局:
// 下面是伪码
<xxxx
android:id="@+id/my_view"
android:layout_width="xxdp"
android:layout_height="xxdp" />
然后搞一个对象:
var animationSet = AnimatorSet()
最后是具体用法,解释写在代码里面:
private fun myAnimationSet() {
// findById等方法拿到view
my_view = **********
// 监听事件,可以在它的生命周期上做操作
animationSet?.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {
}
override fun onAnimationEnd(animation: Animator?) {
}
override fun onAnimationCancel(animation: Animator?) {
}
override fun onAnimationRepeat(animation: Animator?) {
}
})
// 沿x轴缩小一半
val x = ObjectAnimator.ofFloat(my_view, "scaleX", 1.0f, 0.5f)
// 沿y轴缩小一半
val y = ObjectAnimator.ofFloat(my_view, "scaleY", 1.0f, 0.5f)
// 下面连续两行表示从my_view中心缩小
val width = ObjectAnimator.ofFloat(my_view, "pivotX", my_view.width / 2f)
val height = ObjectAnimator.ofFloat(my_view, "pivotY", my_view.height / 2f)
// 透明度
val alpha = ObjectAnimator.ofFloat(my_view, "alpha", 1.0f, 0.0f)
// 同时播放多个动画
animationSet?.playTogether(x, y, width, height, alpha)
// 播放动画持续时间
animationSet?.duration = 100
//开始播放
animationSet?.start()
}
注意:操作会对组件的属性进行修改,如果需要复用需要回到最初
回到最初的代码:
// 下面是伪码
my_view {
my_view.scaleX = 1.0f
my_view.scaleY = 1.0f
my_view.alpha = 1.0f
}