android - TextView 上的闪烁动画与文本同时更改
问题描述
我有一个字符串数组,并希望一次显示一个带有闪烁动画的字符串(在 textView 中闪烁另一个字符串后)。我用下面的代码做了动画部分:
在创建时:
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);
并用这个改变textview文本:
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);
}
});
随意修改...