2012-06-19 127 views
0

我正在創建聖誕節倒計時,我需要每秒倒計時,並需要在畫布上繪製多少天,幾小時,幾分鐘和幾秒到聖誕節。Android CountDownTimer和在畫布上繪製

我可以得到工作輸出,但我覺得有很好的方法來實現這一點,而不是我現在所做的。

這裏是代碼,我刪除喜歡進口,未實現的方法等

public class SnowFall extends Activity { 

SampleView textDraw; 
Paint paint; 
Rect bounds; 
int durationMilli; 
Duration duration; 

static String days; 
static String hours; 
static String minutes; 
static String seconds; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Duration duration = new Duration(); 
    int futureTime = duration.cal_duration(); 
    MyTime tm = new MyTime(futureTime, 1000); 
    tm.start(); 

    textDraw = new SampleView(this); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 
    linearLayout.setBackgroundColor(Color.TRANSPARENT); 
    textDraw.setBackgroundDrawable(getResources().getDrawable(
      R.drawable.background_image_1)); 
    linearLayout.addView(textDraw); 
    setContentView(linearLayout); 

} 

public class MyTime extends CountDownTimer { 

    public MyTime(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 

     SampleView view = new SampleView(getApplicationContext()); 
     int durationInMilliSec = (int) millisUntilFinished; 
     Log.d("Joda Time", String.valueOf(durationInMilliSec)); 

     try { 
      int x = durationInMilliSec/1000; 
      seconds = String.valueOf(x % 60); 
      x /= 60; 
      minutes = String.valueOf(x % 60); 
      x /= 60; 
      hours = String.valueOf(x % 24); 
      x /= 24; 
      days = String.valueOf(x); 

      view.invalidate(); 
     } catch (Exception e) { 

     } 

    } 

} 

private static class SampleView extends View { 
    private Paint mPaint; 
    private float mX; 
    private float[] mPos; 

    private Path mPath; 
    private Paint mPathPaint; 

    private static final int DY = 45; 
    private static final String POSTEXT = "Positioned"; 

    private static void makePath(Path p) { 
     p.moveTo(10, 0); 
     p.cubicTo(100, -50, 200, 50, 300, 0); 
    } 

    private float[] buildTextPositions(String text, float y, Paint paint) { 
     float[] widths = new float[text.length()]; 
     // initially get the widths for each char 
     int n = paint.getTextWidths(text, widths); 
     // now popuplate the array, interleaving spaces for the Y values 
     float[] pos = new float[n * 2]; 
     float accumulatedX = 0; 
     for (int i = 0; i < n; i++) { 
      pos[i * 2 + 0] = accumulatedX; 
      pos[i * 2 + 1] = y; 
      accumulatedX += widths[i]; 
     } 
     return pos; 
    } 

    public SampleView(Context context) { 
     super(context); 
     setFocusable(true); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setTextSize(40); 
     Typeface typeface = Typeface.createFromAsset(context.getAssets(), 
       "font.ttf"); 
     mPaint.setTypeface(typeface); 

     mPos = buildTextPositions(POSTEXT, 0, mPaint); 

     mPath = new Path(); 
     makePath(mPath); 

     mPathPaint = new Paint(); 
     mPathPaint.setAntiAlias(true); 
     mPathPaint.setColor(0x800000FF); 
     mPathPaint.setStyle(Paint.Style.STROKE); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     Paint p = mPaint; 
     float x = mX; 
     float y = 0; 

     if (days != null && hours != null && minutes != null 
       && seconds != null) { 
      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText("There is", x, y, p); 

      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText(days + " Days, " + hours + " Hours", x, y, p); 

      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText(minutes + " Minutes and " + seconds 
        + " seconds", x, y, p); 

      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText("until christmas.", x, y, p); 

      invalidate(); 

     } else { 

      // draw the normal strings 
      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText("Something Wrong", x, y, p); 

      canvas.translate(0, DY); 
      p.setTextAlign(Paint.Align.CENTER); 
      canvas.drawText("Check your device time.", x, y, p); 

     } 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int ow, int oh) { 
     super.onSizeChanged(w, h, ow, oh); 
     mX = w * 0.5f; // remember the center of the screen 
    } 

} 

} 

一些不必要的代碼和有小問題,有多少天算起,因爲它表明,只有10天聖誕。我對Android上的圖形一無所知,在這方面努力工作。

package in.isuru.animation; 

import org.joda.time.DateTime; 
import org.joda.time.Seconds; 

public class Duration { 

public int cal_duration(){ 
    DateTime start = new DateTime(DateTime.now()); 
    DateTime end = new DateTime(2012, 12, 25, 0, 0, 0 ,0); 
    int differenceSeconds = Seconds.secondsBetween(end, start).getSeconds(); 

    return differenceSeconds * 1000; 
} 

} 

問題是在每個Tick上計算天數,小時數,分鐘數和秒數。請告訴我這是好還是還有更好的方法嗎?

回答

0

你的號碼是錯誤的,因爲你用毫秒來溢出int數據類型。 int的最大值是2^31〜2×10^9。聖誕節前還有大約155×10^9毫秒的時間。

只需計算秒數,你會沒事的。或者你可以使用64位的long而不是int。但是你乘以1000來獲得毫秒,然後立即除以1000!這沒有多大意義。

更好的是,通過你的喬達時間!

由於您已經在使用它,爲什麼不構建一個時期?

DateTime start = DateTime.now(); // Note your code makes an unneeded copy here 
DateTime end = new DateTime(2012, 12, 25, 0, 0, 0); 
Period p = new Period(start, end); 

該期限有吸氣劑的年,月,日,小時,分鐘,秒。這將完全避免容易出錯的計算。

3

只是用這個簡單的代碼

countDownTimer = new CountDownTimer(240000, 1000) { 

    public void onTick(long millisUntilFinished) { 
     int seconds = (int) ((millisUntilFinished/1000)%60); 
     int minutes = (int) ((millisUntilFinished/1000)/60); 
     int hours = (int) ((millisUntilFinished/1000)/3600); 
     String startTime ="Time " + String.format("%02d:%02d:%02d",hours,minutes,seconds); 
     txtShowTimer.setText(startTime); 
    } 

    public void onFinish() { 
     txtShowTimer.setText("done!"); 
    } 
}.start(); 
+1

更好地解釋這多一點點,讓OP有更好的想法,爲什麼他沒有這樣做的權利。 – WiSaGaN