代码如下
private fun initPayBtnAnim(view: View) {
val animationSet = AnimatorSet()
view.pivotX = (view.width / 2).toFloat()
view.pivotY = (view.height / 2).toFloat()
val objectAnimator: ObjectAnimator = ObjectAnimator.ofFloat(view, View.SCALE_X, 0.95f, 1f, 1.05f, 0.95f)
objectAnimator.repeatCount = ValueAnimator.INFINITE
objectAnimator.repeatMode = ValueAnimator.RESTART
val objectAnimator1: ObjectAnimator = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0.95f, 1f, 1.05f, 0.95f)
objectAnimator1.repeatCount = ValueAnimator.INFINITE
objectAnimator1.repeatMode = ValueAnimator.RESTART
animationSet.let {
it.duration = 2000
it.playTogether(objectAnimator, objectAnimator1)
it.start()
}
}
调用方法:
binging.imageView.post {
initPayBtnAnim(binging.rlPay)
}
使用post 可以让View 动效以View的中心点的向四周缩放动画,如果在页面刚加载时,直接调用方法,则可能会因为View未加载完成导致动画是以View的左上角向右下缩放效果