Search code examples
androidanimationimageviewsplash-screenduration

Blink imageView for few seconds


In my android application, I have a splash screen which blinks the logo of the app for 3 seconds and then start the login activity. Here is my code:

imgView.postDelayed(new Runnable() {
        @Override
        public void run() {
            final Animation animation = new AlphaAnimation(1, 0);
            animation.setDuration(1000);
            animation.setInterpolator(new LinearInterpolator());
            animation.setRepeatCount(Animation.INFINITE);
            animation.setRepeatMode(Animation.REVERSE);
            imgView.startAnimation(animation);
        }
    }, 3000);
Intent intent = new Intent(SplashscreenActivity.this,LoginActivity.class);
startActivity(intent);

But the image blinks infinitely. How to stop blinking after 3 seconds? I referred some posts but I could not get an exact answer.


Solution

  • You can try this

          final Animation animation = new AlphaAnimation(1, 0);
          animation.setDuration(1000);
          animation.setInterpolator(new LinearInterpolator());
          animation.setRepeatCount(Animation.INFINITE);
          animation.setRepeatMode(Animation.REVERSE);
          imgView.startAnimation(animation);
    
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                  animation .cancel();
                  Intent intent = new Intent(SplashscreenActivity.this, LoginActivity.class);
                  startActivity(intent);
    
              }
          }, 3000);
    

    You can use imgView.clearAnimation() instead of animation.cancel();

    I hope this will help you. thanks