Search code examples
androidanimationloadinganimator

How to Fade In Fade Out text during a loading with Animator?


I want fade in and fade out some text during a loading in my app. For begin i tried this in a loop with counter but it doesn't work. I tried this :

        int i = 0;
        for(i=0; i < 5; i++){
            batteryAnimator = ObjectAnimator.ofFloat(tvBattery, "alpha", 1).setDuration(600);
            batteryAnimator.setStartDelay(200);
            batteryAnimator.start();
            screenAnimator = ObjectAnimator.ofFloat(tvScreen, "alpha", 1).setDuration(600);
            screenAnimator.setStartDelay(1500);
            screenAnimator.start();
            sensorAnimator = ObjectAnimator.ofFloat(tvSensor, "alpha", 1).setDuration(600);
            sensorAnimator.setStartDelay(3000);
            sensorAnimator.start();
            wifiAnimator = ObjectAnimator.ofFloat(tvWifi, "alpha", 1).setDuration(600);
            wifiAnimator.setStartDelay(4500);
            wifiAnimator.start();


            batteryAnimator = ObjectAnimator.ofFloat(tvBattery, "alpha", 0).setDuration(600);
            batteryAnimator.setStartDelay(6000);
            batteryAnimator.start();
            screenAnimator = ObjectAnimator.ofFloat(tvScreen, "alpha", 0).setDuration(600);
            screenAnimator.setStartDelay(6000);
            screenAnimator.start();
            sensorAnimator = ObjectAnimator.ofFloat(tvSensor, "alpha", 0).setDuration(600);
            sensorAnimator.setStartDelay(6000);
            sensorAnimator.start();
            wifiAnimator = ObjectAnimator.ofFloat(tvWifi, "alpha", 0).setDuration(600);
            wifiAnimator.setStartDelay(6000);
            wifiAnimator.start();
    }

I tried to use batteryAnimator.setRepeatedMode(ValueAnimator.RESTART) and battery.Animator.setRepeatCount(ValueAnimator.INFINITE), i think i have to use a thing like this but the texts flash like a christmas tree..

If someone can help me..


Solution

  • Create this method. This will make your view (in your case, your TextView) fade in and out:

        public void fadeInAndOut(final View view) {
            ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha", 0f);
            fadeOut.setDuration(500);
            fadeOut.setInterpolator(new DecelerateInterpolator());
    
            ObjectAnimator fadeIn = ObjectAnimator.ofFloat(view, "alpha", 1f);
            fadeIn.setDuration(500);
            fadeIn.setInterpolator(new DecelerateInterpolator());
    
            AnimatorSet set = new AnimatorSet();
            set.play(fadeIn).after(fadeOut);
    
            set.start();
        }
    

    Then, you need need call this function between some intervals. As an example, I am using CountDownTimer. It will be called 20 times with 1 second intervals.

    new CountDownTimer(20000, 1000) {
    
        public void onTick(long millisUntilFinished) {
          //This will be called every time timer ticks
          fadeInAndOut(/*your TextView goes here*/)
        }
    
        public void onFinish() {
           //Timer is done. 
        }
    
    }.start();