我正在寻找使用Andengine的倒计时教程。这是我需要用app做的事情,即我需要计算到特定日期,当它到达那个日期时它必须显示一些消息,当该日期过去时它必须一次再次显示倒计时到明年的那个日期。如何实现这个。我搜索了各种教程,但我无法找到。希望有人能帮助我。
更新: clocktext = new Text(CAMERA_WIDTH / 4.4f,CAMERA_HEIGHT / 3.6f,this.mFont,“Hello AndEngine!”,this.getVertexBufferObjectManager());
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,30);
thatDay.set(Calendar.MONTH,9); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
//long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
long diff = thatDay.getTimeInMillis()-today.getTimeInMillis(); //result in millis
long days = diff / (24 * 60 * 60 * 1000);
this.remain = days;
if(remain<0){
clocktext = new Text(CAMERA_WIDTH/9.8f, CAMERA_HEIGHT/1.4f,this.mFont, remain + "", this.getVertexBufferObjectManager());
}if(remain==0){
clocktext = new Text(CAMERA_WIDTH/9.8f, CAMERA_HEIGHT/1.4f,this.mFont, "HAPPY HALLOWEEN", this.getVertexBufferObjectManager());
}if(remain>0){
clocktext = new Text(CAMERA_WIDTH/9.8f, CAMERA_HEIGHT/1.4f,this.mFont, remain + "", this.getVertexBufferObjectManager());
}
答案 0 :(得分:0)
以下是代码片段:
package de.goddchen.android.andthrow.library;
import org.anddev.andengine.engine.handler.IUpdateHandler;
public class CountDownTimer implements IUpdateHandler {
private float timerSeconds;
private float timerInterval;
private float lastCall;
private boolean isRunning;
private float timeElapsed;
private iCountDownListener listener;
public interface iCountDownListener {
public void onIntervalElapsed(float timeRemaining, CountDownTimer timer);
public void onCountDownEnded(CountDownTimer timer);
}
public CountDownTimer(float timerSeconds, float timerInterval,
iCountDownListener listener) {
this.timerInterval = timerInterval;
this.timerSeconds = timerSeconds;
this.listener = listener;
this.isRunning = true;
}
@Override
public void onUpdate(float pSecondsElapsed) {
timeElapsed += pSecondsElapsed;
// Log.d(getClass().getSimpleName(), "elapsed: " + timeElapsed);
if (isRunning) {
if (timeElapsed - lastCall > timerInterval) {
listener.onIntervalElapsed(timerSeconds - timeElapsed, this);
lastCall = timeElapsed;
}
if (timeElapsed > timerSeconds) {
listener.onCountDownEnded(this);
cancel();
}
}
}
@Override
public void reset() {
}
public void cancel() {
isRunning = false;
}
}