Want to Display time to the main view of a game?
I am currently developing a simple game for Android where the main action occurs on my class GameView, which is simply an extension of View.
I want to add some text showing the time in the top left corner of my GameView, and would like to know the best way of implementing this?
I used foolowing code to display
public void onTick(long millisUntilFinished) { TIME=(int) (millisUntilFinished/1000);
timeCanvas.drawText("time= "+TIME, 30, 30, timePaint);
invalidate();
}
But it cannot display properly. Its override every count increment. Is there a开发者_StackOverflowny other method to display time?
Also i used text view. but couldnt set TIME as text in this customized viewGive solution or any sample code to display....
This article help you
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
mTimeLabel.setText("" + minutes + ":0" + seconds);
} else {
mTimeLabel.setText("" + minutes + ":" + seconds);
}
mHandler.postAtTime(this,
start + (((minutes * 60) + seconds + 1) * 1000));
} };
to start
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
to stop
mHandler.removeCallbacks(mUpdateTimeTask);
精彩评论