android - 时间递减倒计时

android - countdown with decreasing time

在我的游戏中,用户在 5 秒内单击按钮时会得到一分。现在我希望计时器减少用户获得的每一点的时间 - 例如零分时,用户有五秒的时间,而当他有一分时,他只有 4.5 秒的时间再次点击它并获得第二分。我用for循环解决吗?

public class GameScreen extends Activity {
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Button count = (Button) findViewById(R.id.button1);
        text = (TextView) this.findViewById(R.id.textView3);
        tvscore = (TextView) findViewById(R.id.score);

        timer();
    }

    public void gameover() {
        Intent intent = new Intent(this, GameOverScreen.class);
        startActivity(intent);
    }

    public void onClick (View view) {
        score++;
        tvscore.setText(String.valueOf(score));
        timer();
    }

    public void timer(){
        new CountDownTimer(5000, 10) {
            public void onTick(long millisUntilFinished) {
                text.setText(""+String.format("%02d:%03d",
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
                        ));
                if(animationRunning) {
                    cancel();
                }
            }
            public void onFinish() {
                text.setText("Too slow.");
                gameover();
            }
        }.start();
    }
}

这样做怎么样:

public void timer(float time) {
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}

public void onClick (View view) {
    score++;
    tvscore.setText(String.valueOf(score));
    timer(Math.max(5000-score*500, 2000));
}

我想每次点击(分数)都会减少 500 毫秒的时间...

Limit - Math.max(a, b) 会选择最大值。意思是当5000-score*500低于2000时,它会选择2000毫秒代替

只有定时器方法:

public void timer() {
    float time = Math.max(5000-score*500, 2000)
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}