Apply one animation to multiple views at the same time

前端 未结 3 417
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  • 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()
                }
    
    0 讨论(0)
  • 2020-12-30 00:28

    So I guess this just isn't possible, so I created a helper method to just apply the same animation to a list of views:

    public void doRotations(ArrayList<View> views, int start, int end, int xprop, float xscale, int yprop, float yscale, int duration, Boolean fillAfter){
    
        for(int i = 0; i < views.size(); i++){
            RotateAnimation temp = new RotateAnimation(start, end, xprop, xscale, yprop, yscale);
            temp.setDuration(duration);
            temp.setFillAfter(fillAfter);
            views.get(i).startAnimation(temp);
        }
    }
    

    Definitely a hack, but I guess thats all I'm able to do right now

    0 讨论(0)
  • 2020-12-30 00:39

    Do it like this:

    ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 100f);
    arrayListObjectAnimators.add(anim);
    
    ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "x", 0f);
    arrayListObjectAnimators.add(anim1);
    
    ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]);
    AnimatorSet animSetXY = new AnimatorSet();
    animSetXY.playTogether(objectAnimators);
    animSetXY.duration(1000);
    animSetXY.start();
    
    0 讨论(0)
提交回复
热议问题