Oliver Carneiro
Oliver Carneiro

Reputation: 45

Fade in and out an image constantly in Android app

I'm trying to make an image fade in and out constantly, but it just fade in and out one time. How can I make it repeat constantly? Here's the code:

Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setDuration(1000);

    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setStartOffset(1000);
    fadeOut.setDuration(1000);

    AnimationSet animation = new AnimationSet(true);
    animation.addAnimation(fadeIn);
    animation.addAnimation(fadeOut);
    ImageView loading = (ImageView)findViewById(R.id.loading);
    loading.startAnimation(animation);

Upvotes: 0

Views: 545

Answers (3)

R. Zagórski
R. Zagórski

Reputation: 20268

Try to use the method setRepeatMode(int repeatMode) from Animation class.

Upvotes: 0

pdegand59
pdegand59

Reputation: 13029

With Animator, it's pretty easy :

Animator alphaAnimator = ObjectAnimator.ofFloat(loading, View.ALPHA, 0f, 1f);
alphaAnimator.setDuration(1000);
alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
alphaAnimator.start();

Upvotes: 1

Max Plakhuta
Max Plakhuta

Reputation: 310

You should repeat your animation:

animation.setRepeatCount(Animation.INFINITE);

Upvotes: 0

Related Questions