Apply one animation to multiple views at the same time

前端 未结 3 421
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 23:40

So Id like to rotate a handful of views all at the same time, all using the same rotation specs. The issue is that for some reason the rotation acts differently for the sec

3条回答
  •  萌比男神i
    2020-12-30 00:21

    I was able to do this in Kotlin by programmatically creating one AnimatorSet.
    1. Create an ArrayList of the views you want to animate

     var viewList = arrayListOf(view1,view2,view3)
    

    2. Loop through the ArrayList and create a growing AnimatorSet

    var ix = 0
    var anim = AnimatorSet()
    var viewList = arrayListOf(view1,view2,view3)
            viewList.forEach{
            // Initiate the animator set with one ObjectAnimator
                if(ix == 0){
                    anim = AnimatorSet().apply {
                        play(ObjectAnimator.ofFloat(it, "rotation", 0F, 360F))
                    }
                }
            // Add one ObjectAnimator at a time to the growing AnimatorSet
                else{
                    var currentAnim = ObjectAnimator.ofFloat(it,"rotation",0F,360F)
                    anim = AnimatorSet().apply {
                        play(anim).with(currentAnim)
                    }
                }
                ix++
            }
    

    3. Start the animation

    button.setOnClickListener {
                AnimatorSet().apply {
                    play(anim)
                    start()
                }
    

提交回复
热议问题