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()
  }
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) }

猜你喜欢

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