exkulo
exkulo

Reputation: 1

Why my views start animation in the same time?

I want my views appearing gradually. So I use Timer to delay the time the views appear. However it do not work. Strangely, the views appear in the same time! Here's my code.

public static void startAnimations(final View... views) {
        //assume 720
        int screenWidth = 720;
        final TranslateAnimation translateAnimation = new TranslateAnimation(-screenWidth / 2, 0, 0, 0);
        final AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1);
        translateAnimation.setDuration(800);
        alphaAnimation.setDuration(800);

        mViewIndex = 0;
        final Timer timer = new Timer();
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (mViewIndex >= views.length) {
                    timer.cancel();
                    mViewIndex = 0;
                    return;
                }
                View view = views[mViewIndex];
                final AnimationSet animationSet = new AnimationSet(true);
                animationSet.setDuration(800);
                animationSet.setInterpolator(new AccelerateInterpolator());
                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(translateAnimation);
                view.startAnimation(animationSet);
                mViewIndex++;
            }
        };

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x11);
            }
        }, 0, 300);
    }

Upvotes: 0

Views: 36

Answers (1)

user4571931
user4571931

Reputation:

Try to use setAnimationListener in that you will have following @override method

@Override
public void onAnimationEnd(Animation animation) {

}

so in that you can show next animation

Upvotes: 1

Related Questions