使用相同的动画隐藏和显示多个视图

Hiding and showing multiple views using the same animation

我想同时为多个视图应用动画,如何将所述动画应用到多种类型的视图(按钮、图像视图和其他视图)?

如果您想要同时在多个视图上应用动画,只需依次对这些视图调用 startAnimation 方法即可。他们将同时

//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);

或者如果您有很多意见:

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);

也就是说,假设您想同时为多个视图设置动画,如果您正在做的是一个接一个地进行,我们将深入研究动画侦听器,那是另一回事了

您可以使用对象动画师。 link 中有一个简单的示例,您也可以使用它。

您也可以使用这个 tutorial

改为使用 ValueAnimator,并在 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());
            }
        }
    });

你可以创建一个大的AnimatorSet() that includes several ObjectAnimators. This allows you to play all animations at the same time with no delay. See my answer here