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

Nob*_*oby 26 animation android

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

现在它以顺序的方式工作.也就是说,在完成一个它的另一个之后.

这是我的代码.

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

            }
        });
Run Code Online (Sandbox Code Playgroud)

Gop*_*fur 14

我想你需要用一个AnimationSet.

来自doc:

表示应该一起播放的一组动画.每个动画的变换一起组成一个变换.

在这里你可以看到AnimationSet应该如何.

  • 我认为动画集是动画的组合,而不是单独视图上的单独动画. (7认同)

Gui*_*lla 5

在您的活动中

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);
Run Code Online (Sandbox Code Playgroud)

然后,在 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>
Run Code Online (Sandbox Code Playgroud)