Подтвердить что ты не робот

Как запустить две анимации одновременно в 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));

            }
        });

Спасибо заранее!

4b9b3361

Ответ 1

Я думаю, вам нужно использовать AnimationSet

http://developer.android.com/reference/android/view/animation/AnimationSet.html

Из документа:

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform.

Этот вид ссылки показывает, как предполагается создание/выполнение AnimationSet: http://groups.google.com/group/android-developers/browse_thread/thread/9df24ac25f01cee9/6e0eac7802a541e3

Ответ 2

В вашей деятельности

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>

Ответ 3

Способом решения этой проблемы является использование AnimatorSet с Animator. Если вас беспокоит обратная совместимость, вы можете использовать библиотеку NineOldAndroids, чтобы вернуть API полностью на Android 1.0

Ответ 4

Я решил запустить вторую анимацию в событии onAnimationStart первой анимации. В моем примере правильная компоновка, заменяющая левую, обе они движутся влево в одно и то же время. Я использую свойство animate класса View http://developer.android.com/reference/android/view/View.html#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);
                        }
                    });
        }
});