Repeat AnimatorSet

后端 未结 7 1594
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 09:19

Is there a simple way to repeat a Android AnimatorSet (infinite)? Can I set a AnimationListener and restart the AnimatorSet by calling

7条回答
  •  被撕碎了的回忆
    2021-02-05 10:06

    No, you can't repeat AnimatorSet, you can only repeat of a single ObjectAnimator/ ValueAnimator

    But you can use PropertyValuesHolder, so that you can make an ObjectAnimator from a set of PropertyValuesHolder

    here's example

    val translateY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, -100f, 100f)
    val alpha = PropertyValuesHolder.ofFloat(View.ALPHA, 0.3f, 0.7f)
    val scaleX = PropertyValuesHolder.ofFloat(View.SCALE_X, 0.8f, 1.2f)
    val scaleY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0.8f, 1.2f)
    // and more
    
    ObjectAnimator.ofPropertyValuesHolder(myView, translateY, alpha, scaleX, scaleY).apply {
        interpolator = AccelerateDecelerateInterpolator()
        // duration of each animation
        duration = 500L
        // repeat infinite count (you can put n times)
        repeatCount = ValueAnimator.INFINITE
        // reverse animation after finish
        repeatMode = ValueAnimator.REVERSE
        // start animation
        start()
    }
    

提交回复
热议问题