2014-10-07 49 views
0

我正在尋找一個倒計時教程使用Andengine.This是我需要做的應用程序即我需要計數,直到具體日期,當它達到該日期,它必須顯示一些消息,當通過它的日期必須再次顯示倒計時,直到明年的這一天。如何實現this.i搜索各種教程,但無法找到。希望有人會幫助我。 clock)= new Text(CAMERA_WIDTH/4.4f,CAMERA_HEIGHT/3.6f,this.mFont,「Hello AndEngine!」,this.getVertexBufferObjectManager());倒計時日期/天使用Andengine

 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

只是一個替代方案:在andengine也ü可以實現簡單的Java或Android countDown方法Timeer,或者更確切地說,u可以使用chrometer太.. – KOTIOS 2014-10-07 13:25:58

+0

@mtetno感謝答案。如何設置確切日期從日曆和當前日期說30/10 /年,我想得到的差異,即Days.And到達那天我想顯示一條消息,當日期通過我想反櫃開始顯示剩餘的日子,直到明年。 – 2014-10-07 13:31:33

+0

@mtetno更新了我試過的代碼。 – 2014-10-07 13:35:06

回答

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; 
    } 

}