在文本同时更改的情况下,在TextView上闪烁动画



我有一个字符串数组,希望通过眨眼动画一次显示一个字符串(眨眼后在textView中显示另一个字符串(。我用下面的代码做了动画部分:

OnCreate:

TextView subTitle=findViewById(R.id.subTitle);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(2500); 
anim.setStartOffset(0);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
subTitle.startAnimation(anim);

并用以下内容更改文本视图文本:

Handler h = new Handler();
int delay = 2500;
Runnable runnable;
@Override
protected void onResume() {
h.postDelayed(runnable = new Runnable() {
public void run() {
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
h.postDelayed(runnable, delay);
}
}, delay);
super.onResume();
}

但是textView的文本不会在眨眼的同时发生变化。有没有一种方法可以让我做到这一点?

您可以执行以下操作。。。

anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//if set animation count to once
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
anim.start();
}
@Override
public void onAnimationRepeat(Animation animation) {
// Or if set repeat count to infinite
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
}
});

根据需要修改。。。

最新更新