我有一个应用程序,为包含计时器的用户创建问题(总和)。计时器到达零后,会向用户显示分数以及 再次播放的TextView
?
代码运行正常,直到我实现闪烁/闪烁(动画设置TextView颜色然后透明)。单击Play Again?
后,应用会停止但不会崩溃。我认为这是因为没有调用playAgain()
方法?
我想保留面向对象的方法,这就是我为动画创建一个名为flashPlay()
的单独方法的原因。
以下是playAgain()
代码:
public void playAgain (View v) {
score = 0;
numOfSums = 0;
timeTextView.setText("30");
scoreTextView.setText("0 : 0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
button0.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
playAgainButton.setEnabled(true);
createQuestion();
textViewTimesUp.setVisibility(View.INVISIBLE);
new CountDownTimer(3100, 1000){
@Override
public void onTick(long millisUntilFinished) {
timeTextView.setText(String.valueOf(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
button0.setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
playAgainButton.setVisibility(View.VISIBLE);
timeTextView.setText("0");
textViewTimesUp.setVisibility(View.VISIBLE);
flashPlay(); //calling the flashPlay() method.
if (score >= 1 && numOfSums >= 1) {
int percent =((score * 100) / numOfSums);
resultTextView.setText("Score: " + percent + "%");
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
} else {
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
}
}
}.start();
}
这是flashPlay()代码:
public void flashPlay () {
final ObjectAnimator colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
playAgainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
colorAnim.end();
}
});
}
有人可以告诉我代码的错误吗。
谢谢。
答案 0 :(得分:0)
在我的应用中,我只是用这个动画使我的textView闪烁:
public static Animation blinkAnim() {
// Configure your animation properties here
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(550);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(10);
animation.setRepeatMode(Animation.REVERSE);
return animation;
}
您可以通过以下方式进行攻击:
// get the animation
Animation anim = blinkAnim();
// start the animation
myTextView.startAnimation(anim);
答案 1 :(得分:0)
ObjectAnimator colorAnim = null;
onCreate(){
...
playAgainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
if(colorAnim != null && colorAnim.isStarted()){
colorAnim.end();
//playAgain(v); ?
}
}
});
...
}
...
public void flashPlay () {
colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}