Android 实现属性动画的方法封装
2023-08-06 本文已影响0人
背锅TV丶伴奏大师
/**
* 1.使用属性动画将控件targetView平移到控件destinationView位置
* 2.targetView大小缩放为destinationView大小
* 3.targetView的透明度由1f到0f
* 该方法要在页面布局完成之后才能使用
*
* @param targetView
* @param destinationView
* @param duration
*/
private fun translateAnim(targetView: View, destinationView: View, duration: Long) {
val animatorSet = AnimatorSet()
//平移动画
val translationX =
ObjectAnimator.ofFloat(targetView, "translationX", 0f, destinationView.x - targetView.x)
val translationY =
ObjectAnimator.ofFloat(targetView, "translationY", 0f, destinationView.y - targetView.y)
//缩放动画
val scaleX = ObjectAnimator.ofFloat(
targetView,
"scaleX",
1.0f,
destinationView.width * 1.0f / targetView.width
)
val scaleY = ObjectAnimator.ofFloat(
targetView,
"scaleY",
1.0f,
destinationView.height * 1.0f / targetView.height
)
//透明动画,透明度从1变成0
val alpha = ObjectAnimator.ofFloat(targetView, "alpha", 1f,0f)
//设置控件缩放中心为控件左上角0,0
targetView.pivotX = 0f
targetView.pivotY = 0f
//设置多个ObjectAnimator
animatorSet.playTogether(translationX, translationY, scaleX, scaleY,alpha)
//设置插值器
animatorSet.interpolator = AccelerateInterpolator()
//设置动画时长
animatorSet.duration = duration
//执行动画
animatorSet.start()
}