如何在android中同时启动两个动画?

如何在android中同时启动两个动画?,android,animation,Android,Animation,我有两个线性布局,我想在这两个布局上同时执行两个不同的动画 现在它以顺序的方式工作。i、 e,完成一个后,开始另一个 这是我的密码 Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,

我有两个线性布局,我想在这两个布局上同时执行两个不同的动画

现在它以顺序的方式工作。i、 e,完成一个后,开始另一个

这是我的密码

    Animation inFromRight = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, +0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromRight.setDuration(500);
            inFromRight.setInterpolator(new AccelerateInterpolator());

    Animation outtoLeft = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, -1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            outtoLeft.setDuration(500);
            outtoLeft.setInterpolator(new AccelerateInterpolator());

    @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.menu:
                            mainLayout.startAnimation(outtoLeft);
                sideBar.startAnimation(inFromRight);                
                break;
            }
        }

outtoLeft.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mainLayout
                        .setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT, 40));

            }
        });

我认为您需要使用
动画集

从:

表示应一起播放的一组动画。这个 将每个单独动画的变换组合到一起 单个变换


您可以看到
动画集应该是什么样子的。

解决此问题的方法是对对象使用。如果您担心向后兼容性,您可以在活动中使用NineodelDroids库将API一直带回Android 1.0

ImageView reusableImageView = (ImageView)findViewById(R.id.imageView1);
reusableImageView.setImageResource(R.drawable.flag);
reusableImageView.setVisibility(View.VISIBLE);
Animation an =  AnimationUtils.loadAnimation(this, R.anim.yourAnimation);
reusableImageView.startAnimation(an);
然后,在yourAnimation.xml中定义所有需要的动画

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="2.0"
    android:toYScale="2.0"
    android:duration="2500" />
<scale
    android:startOffset="2500"
    android:duration="2500"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.5"
    android:toYScale="0.5" />
<rotate
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="5000" />
</set>

我在第一个动画的onAnimationStart事件中解决了启动第二个动画的问题。在我的示例中,右布局替换左布局,两个布局同时向左移动。我使用一个视图类的animate属性,该属性从API v.12开始就可用

leftLayout.animate()
    .translationX(-leftLayout.getWidth()) // minus width
    .setDuration(300)
    .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            rightLayout.animate()
                    .translationX(-leftLayout.getWidth()) // minus width
                    .setDuration(300)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            leftLayout.setVisibility(View.GONE);
                            leftLayout.setTranslationX(0f); // discarding changes
                            rightLayout.setTranslationX(0f);
                        }
                    });
        }
});

这是你的全部代码吗?写这两个动画的方式应该同时开始。不,我也有动画监听器。完成一个动画后,我正在执行其他一些UI操作,这是否会影响。。?我已经更新了代码。我认为动画集是动画的组合,而不是单独视图上的单独动画。你是对的,我误读了这个问题,以为你在同一个对象上执行动画。第二个链接断了,一直给我一个禁止的警告