如何创建一个textPaint作为倒计时器来控制游戏结束? 我曾尝试使用带有drawText的countDownTimer做类似的事情,它可以在surfaceview中显示,但不会为每个onTick更新。
public class CfView extends SurfaceView {
/** The activity object. */
private final Activity activity;
/** The answer object. */
private static Answer answer;
private static int score;
/** The InputPanel object. */
private static InputPanel inputPanel;
/** The AnswerPanel object. */
private static AnswerPanel answerPanel;
/** Keyboard panel height. */
private static final float INPUT_PANEL_HEIGHT_WEIGHT = 0.7f;
/** Answer panel height. */
private static final float ANSWER_PANEL_HEIGHT_WEIGHT = 0.3f;
private Timer timer;
private String countDownTime;
private boolean gameOver;
private UserInput userInput = new UserInput();
private CountDownTimer cdTimer;
/** Saving and handling of user input of touch events. */
private class UserInput {
/** Whether there is a user input present. */
boolean present = false;
/** Action of the user input {@link MotionEvent}. */
int action;
/** x, y positions of the user input {@link MotionEvent}. */
int x, y;
synchronized void save(MotionEvent event) {
present = true;
action = event.getAction();
x = (int) event.getX();
y = (int) event.getY();
handle();
}
synchronized void handle() {
int pos = -1;
if (present) {
cdTimer.start();
if (action == MotionEvent.ACTION_UP) {
int[] clickedBallData = inputPanel.checkballStroked(x, y);
if (clickedBallData != null) {
pos = clickedBallData[0];
int value = clickedBallData[1];
if (answer.isCorrect(value)) {
if (answer.isHCF(value)) {
score += 500;
} else {
score += 100;
}
} else {
score -= 100;
if (score < 0)
score = 0;
}
inputPanel.updateInputPanel(pos, answer);
}
}
drawGameComponent();
present = false;
}
}
}
private class AnimationTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
Log.d("thread", "run");
userInput.handle();
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
answerPanel.drawOn(canvas);
inputPanel.drawOn(canvas);
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(50);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Score : " + score, getWidth() * 0.7f, 50,
textPaint);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(50);
textPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText("Time : " + countDownTime + "s", 0, 50,
textPaint);
if (gameOver) {
textPaint.setTextSize(2 * 30);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Times up", getWidth() / 2,
getHeight() / 2, textPaint);
canvas.drawText("Score : " + score, getWidth() / 2,
getHeight() / 2 + (2 * 30), textPaint);
}
getHolder().unlockCanvasAndPost(canvas);
}
}
}
public CfView(Activity activity) {
super(activity);
setFocusableInTouchMode(true); // For getting key events
cdTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
countDownTime = String.valueOf(millisUntilFinished / 1000);
invalidate();
}
public void onFinish() {
countDownTime = 0 + "";
gameOver = true;
cancel();
}
};
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
userInput.save(event);
return true;
}
});
this.activity = activity;
timer = new Timer();
timer.schedule(new AnimationTask() {
@Override
public void run() {
while (getWidth() == 0)
; // Wait for layout
newGame(true);
}
}, 0);
}
public void resume() {
if (timer == null)
timer = new Timer();
timer.schedule(new AnimationTask(), 0, 0);
}
public void pause() {
timer.cancel();
timer = null;
}
private void drawGameComponent() {
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
answerPanel.drawOn(canvas);
inputPanel.drawOn(canvas);
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(50);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Score : " + score, getWidth() * 0.7f, 50,
textPaint);
getHolder().unlockCanvasAndPost(canvas);
}
}
public void drawTimeText() {
Canvas canvas = getHolder().lockCanvas();
Paint textPaint = new Paint();
if (canvas != null) {
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(50);
textPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText("Time : " + countDownTime + "s", 0, 50, textPaint);
if (gameOver) {
textPaint.setTextSize(2 * 30);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Times up", getWidth() / 2, getHeight() / 2,
textPaint);
canvas.drawText("Score : " + score, getWidth() / 2, getHeight()
/ 2 + (2 * 30), textPaint);
}
getHolder().unlockCanvasAndPost(canvas);
}
}
public void newGame(boolean newGame) {
score = 0;
gameOver = false;
if (newGame) {
answer = new Answer(activity);
Log.d("CF", getHeight() + ";" + getWidth());
answerPanel = new AnswerPanel(activity, 0, 0, getWidth(),
(int) (getHeight() * ANSWER_PANEL_HEIGHT_WEIGHT), answer);
inputPanel = new InputPanel(activity, 0,
(int) (getHeight() * (ANSWER_PANEL_HEIGHT_WEIGHT)),
getWidth(), (int) (getHeight()), answer);
} else {
answer.newNumber();
inputPanel.resetInputPanel();
answerPanel.resetAnswerPanel(answer);
}
drawGameComponent();
}
public void updateCategory(Answer.Level level) {
answer.updateCategory(level);
newGame(false);
}
@Override
protected boolean verifyDrawable(Drawable who) {
return true;
}
@Override
public void invalidateDrawable(Drawable drawable) {
}
}