如何逐个动画3个文本视图?

时间:2018-04-02 12:03:47

标签: android android-animation

我有3个文本视图,我需要逐个按顺序对它们进行动画处理。

当第一个textview结束动画时,第二个应该开始,当第二个结束时第三个应该开始。

这是我的动画

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

这是我试过的代码

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

2 个答案:

答案 0 :(得分:1)

这是经过更新和测试的代码

我认为你应该这样做:

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

答案 1 :(得分:0)

我认为AnimationSet可以作为动画的组合,而不是单独视图上的单独动画。因此,如果您可以使用ObjectAnimator为多个视图设置动画,如下所示:并使用AnimatorSet一起启动动画,它可能会起作用。

注意:我没有尝试过,确定是否有效。但无论如何你可以尝试一下。

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