美文网首页
Android 实现属性动画的方法封装

Android 实现属性动画的方法封装

作者: 背锅TV丶伴奏大师 | 来源:发表于2023-08-06 19:36 被阅读0次
/**
     * 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()
    }

相关文章

网友评论

      本文标题:Android 实现属性动画的方法封装

      本文链接:https://www.haomeiwen.com/subject/qiswpdtx.html