如何在使用 Animator 加载期间淡入淡出文本?

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

我想在我的应用程序加载期间淡入和淡出一些文本。 首先,我在一个带有计数器的循环中尝试了这个,但它不起作用。 我试过了:

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

我尝试使用 batteryAnimator.setRepeatedMode(ValueAnimator.RESTART) 和 battery.Animator.setRepeatCount(ValueAnimator.INFINITE),我想我必须使用这样的东西,但文本闪烁一棵圣诞树..

如果有人可以帮助我..

创建此方法。这将使您的视图(在您的情况下,您的 TextView)淡入淡出:

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

然后,你需要在一些间隔之间调用这个函数。例如,我使用 CountDownTimer。它将以 1 秒的间隔调用 20 次。

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