joker
joker

Reputation: 15

How to animate 3 textviews one by one?

I have 3 text views I need to make them animate in the order one by one.

When 1st textview ends the animation the second one should start and when the second ones end the third should start.

this is my animation

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="-5"
    android:toXDelta="15"
    android:repeatCount="15"
    android:repeatMode="reverse"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="70" />
</set>

this is the code that i tried

final Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation);
    animation.reset();
    tv1.clearAnimation();
    tv1.startAnimation(animation);

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

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            tv1.clearAnimation();
            tv2.clearAnimation();
            tv2.startAnimation(animation);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            tv2.clearAnimation();
        }
    });

Upvotes: 0

Views: 242

Answers (2)

MusabAlothman
MusabAlothman

Reputation: 78

here is the updated and tested code

I think you should do something like this :

//these should be global variables

List<View> views = new ArrayList<>();
List<Animation> animations = new ArrayList<>();
int i = 0;
//end of global variables

    views.add(findViewById(R.id.tv1));
    views.add(findViewById(R.id.tv2));
    views.add(findViewById(R.id.tv3));

    animations.add(AnimationUtils.loadAnimation(this, R.anim.animation));
    animations.add(AnimationUtils.loadAnimation(this, R.anim.animation));
    animations.add(AnimationUtils.loadAnimation(this, R.anim.animation));


    final Animation.AnimationListener listener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            i++;
            if (i < views.size()) {
                views.get(i).startAnimation(animations.get(i));
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    };

    animations.get(0).setAnimationListener(listener);
    animations.get(1).setAnimationListener(listener);
    animations.get(2).setAnimationListener(listener);



    views.get(0).startAnimation(animations.get(0));

Upvotes: 1

Sunil Sunny
Sunil Sunny

Reputation: 3994

I think AnimationSet works as combination of animations, not as separate animation on separate views. So if You can use the ObjectAnimator for animating the multiple view as follows: And use the AnimatorSet to start the animations together it may work.

Note : I have not tried it and sure if this works. But you can give it a try anyway.

ArrayList<ObjectAnimator> arrayListObjectAnimators = new ArrayList<ObjectAnimator>(); //ArrayList of ObjectAnimators

ObjectAnimator anim1 = ObjectAnimator.ofFloat(tv1, "translationX", 100f);
arrayListObjectAnimators.add(anim1);

ObjectAnimator anim2 = ObjectAnimator.ofFloat(tv2, "translationX", 100f);
arrayListObjectAnimators.add(anim2);

ObjectAnimator anim3 = ObjectAnimator.ofFloat(tv3, "translationX", 100f);
arrayListObjectAnimators.add(anim3);
...

ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]);
AnimatorSet animSet = new AnimatorSet();
animSet.playTogether(objectAnimators);
animSet.setDuration(1000);//1sec
animSet.start();

Upvotes: 0

Related Questions