Android动画集合播放

  companion object {
        /**
         * 动画集合播放方式一
         */
        fun buttonAnimator(btn: View) {
            var animator = ObjectAnimator.ofFloat(btn_pay, "button", 100f, 80f, 100f)//如果没有属性,就相当于valueAnimator,用法类似
            animator.duration = 300
            animator.addUpdateListener { animation ->
                run {
                    //                var fraction = animation.animatedFraction
                    var value: Float = animation.animatedValue as Float
                    btn.alpha = value / 100
                    btn.scaleX = value / 100
                    btn.scaleY = value / 100
                }

            }
            animator.start()
        }

        /**
         * 动画集合播放方式二
         */
        fun viewAnimator(btn: View) {
            var animator = ValueAnimator.ofFloat(100f, 80f, 100f)
            animator.duration = 300
            animator.addUpdateListener { animation ->
                run {
                    //                var fraction = animation.animatedFraction
                    var value: Float = animation.animatedValue as Float
                    btn.alpha = value / 100
                    btn.scaleX = value / 100
                    btn.scaleY = value / 100
                }

            }
            animator.start()
        }

        /**
         * 动画集合播放方式三
         */
        fun viewPropertyAnimator(view: View) {
            var PropertyValues1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f, 1f)
            val PropertyValues2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f, 1f)
            val PropertyValues3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f, 1f)
            var animator = ObjectAnimator.ofPropertyValuesHolder(view, PropertyValues1, PropertyValues2, PropertyValues3)
            animator.duration = 300
            animator.start()
        }

        /**
         * 动画集合播放方式四
         */
        fun viewSetAnimator(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)
            var animatorSet = AnimatorSet()
            animatorSet.duration = 300
            animatorSet.playTogether(animator1, animator2, animator3)
            animatorSet.start()
        }

        /**
         * 揭露动画 Reveal Effect
         * 从中心扩散
         */
        fun revealAnimatorCenter(view: View) {
            var animator = ViewAnimationUtils.createCircularReveal(view
                    , view.width / 2, view.height / 2//扩散的中心点
                    , 0f, view.height.toFloat())//扩散半径
            animator.duration = 300
            animator.interpolator = AccelerateInterpolator()
            animator.start()
        }

        /**
         * 揭露动画 Reveal Effect
         * 从左上角位置扩散
         */
        fun revealAnimatorStart(view: View) {
            var animator = ViewAnimationUtils.createCircularReveal(view, 0, 0, 0f, Math.hypot(view.width.toDouble(), view.height.toDouble()).toFloat())
            animator.duration = 300
            animator.interpolator = AccelerateInterpolator()
            animator.start()
        }

        /**
         * 设置加速器
         */
        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()
        }

        fun vibratorAnimator(view: View) {
            var animator = ObjectAnimator.ofFloat(view, "translationX", -100f, 0f, 100f, 0f)
            animator.duration = 500
            animator.start()
            Vibrate(view.context as Activity, 500)
        }

        /**
         * final Activity activity :调用该方法的Activity实例 long milliseconds :震动的时长,单位是毫秒
         * long[] pattern :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
         * boolean isRepeat : 是否反复震动,如果是true,反复震动,如果是false,只震动一次
         */
        private fun Vibrate(activity: Activity, milliseconds: Long) {
            val vib = activity.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
            vib.vibrate(milliseconds)
        }

        private fun Vibrate(activity: Activity, pattern: LongArray,
                            isRepeat: Boolean) {
            val vib = activity.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
            vib.vibrate(pattern, if (isRepeat) 1 else -1)
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值