我有一个String数组,并希望通过眨眼动画一次显示一个String(眨眼后在textView中显示另一个String)。我用下面的代码制作了动画部分:
在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);
并使用以下方法更改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的文本不会同时闪烁。有什么办法可以做到这一点?
答案 0 :(得分:0)
您可以像下面这样...
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);
}
});
根据需要进行修改...