3

I want to apply an animation for multiple views at the same time, how can I apply said animation to several types of views (buttons, imageviews, and other views)?

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
MBH
  • 16,271
  • 19
  • 99
  • 149
  • You have to be more specific about this, xml animation, animator, what kind of animation you want? – Alejandro Cumpa May 20 '15 at 19:46
  • Please clarify your question. Do you want to animate (say... fade-in) multiple views at the same time? One after another? – Juan Cortés May 20 '15 at 19:46
  • You can't. Unless you implement a custom Animation or you use a `ValueAnimator` and set the values to the `Views` manually. Most of all you shouldn't have to do that. You can choreograph multiple animations - whether they are the same or all different with an `AnimatorSet`, but even that is unnecessary for the most basic use case. My point is: there is nothing wrong with multiple `Views` using different `Animator` instances - even if they are all performing the exactly same animation. – Xaver Kapeller May 20 '15 at 19:48

4 Answers4

1

If what you want is to apply an animation on several views at the same time, simply call the startAnimation method on those views one after the other. They will be simultaneous

//Get your views
View view1 = findViewById(R.id.view1);
View view2 = findViewById(R.id.view2);
View view3 = findViewById(R.id.view3);

//Get your animation
Animation youranimation = AnimationUtils.loadAnimation(this, R.anim.animationid);

//Start animations
view1.startAnimation(youranimation);
view2.startAnimation(youranimation);
view3.startAnimation(youranimation);

Or if you have a lot of views:

Animation youranimation = AnimationUtils.loadAnimation(this, R.anim.animationid);
int[] viewIds = new int[]{R.id.view1,R.id.view2,R.id.view3,R.id.view4};
for(int id : viewIds) findViewById(id).startAnimation(youranimation);

That is, assuming you want to animate several views at the same time, if what your're doing is one after the other we would dive into animation listeners and that's another story

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
1

You may use object animators. There is an easy example in that link which you can also use.

You may also use this tutorial.

Community
  • 1
  • 1
ysnsyhn
  • 457
  • 1
  • 6
  • 12
1

Use ValueAnimator instead, and set views property in onAnimationUpdate.

    mShowAnimator = ValueAnimator.ofFloat(0f, 1f);
    mShowAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            for (View v : mTargetViews) {
                v.setAlpha((Float)animation.getAnimatedValue());
            }
        }
    });
ieatbyte
  • 133
  • 2
  • 6
0

You can create one large AnimatorSet() that includes several ObjectAnimators. This allows you to play all animations at the same time with no delay. See my answer here.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61