我希望在加载应用程序时淡入和淡出一些文本。首先,我尝试使用计数器循环进行此操作,但无法正常工作。我试过了:
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();