Android动画Interpolator和自定义动画轨迹

 /**
         * 设置加速器
         */
        fun interpolatorAnimator(view: View) {
            val animator1 = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 1f)
            val animator2 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.5f, 1f)
            val animator3 = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.5f, 1f)
            val animator4 = ObjectAnimator.ofFloat(view, "translationX", 0f, 300f,0f)
            val animator5 = ObjectAnimator.ofFloat(view, "translationY", 0f, 500f,0f)
            var animatorSet = AnimatorSet()
            animatorSet.duration = 300
            animatorSet.interpolator = AccelerateDecelerateInterpolator()
//            animatorSet.interpolator=AccelerateInterpolator()
//            animatorSet.interpolator=AnticipateInterpolator()
            animatorSet.playTogether(animator1, animator2, animator3, animator4, animator5)
            animatorSet.start()
        }

        /**
         * 实现抛物线效果
         * x:匀速
         * y:加速度 y = 1/2*g*t*t
         * 使用估值器实现
         */
        fun parabolaAnimator(view: View) {
            var valueAnimator = ValueAnimator()
            valueAnimator.duration = 3000
            valueAnimator.setObjectValues(PointF())
            //估值器--定义计算规则
            valueAnimator.setEvaluator { fraction, startValue, endValue ->
                run {
                    var pointF = PointF()
                    pointF.x = 200f * (fraction * valueAnimator.duration / 1000)//x=v*t
                    pointF.y = 0.5f * 9.8f * (fraction * valueAnimator.duration / 1000) * (fraction * valueAnimator.duration / 1000)//y = 1/2*g*t*t
                    pointF
                }
            }
            valueAnimator.addUpdateListener { animation ->
                run {
                    var animatedValue = animation.animatedValue as PointF
                    view.x = animatedValue.x
                    view.y = animatedValue.y
                }
            }
            valueAnimator.start()
        }

猜你喜欢

转载自blog.csdn.net/chenli_001/article/details/80028061