我有一個簡單的圖形應用程序,它在屏幕上繪製一些東西。之前,當我在onDraw()
中撥打invalidate()
而沒有檢查時,我沒有任何問題。在我的應用程序onDraw()
每秒鐘被調用很多次(我通過日誌瞭解到),但我不想每秒調用它多次,因爲我只想在每秒後更新屏幕。所以我試圖比較以前和現在的秒數 - 如果它們相同,我不會撥打invalidate()
;如果第二次更改,我會撥打invalidate()
。這裏是我的代碼:秒不在onDraw更新()
protected void onDraw(Canvas canvas) {
// Create date to check current second
Date date = new Date();
canvas.drawColor(Color.WHITE);
drawArcs(canvas, mBigOval, mUseCenters[0], mPaints[0]);
//Get the current second
curSec = date.getSeconds();
//compare if its same as lastSec which is updated in drawArcs() which i called before
do {
Log.d("Graphics","curSec="+curSec+" lastSec="+lastSec);
curSec = date.getSeconds();
} while(curSec == lastSec);
Log.d("Graphics","Calling invalidate() and count="+(++count));
invalidate();
}
與上面的代碼,如果我的應用程序時,在第30秒啓動,則日誌顯示:
06-25 10:25:52.257: D/Graphics(14711): curSec=30 lastSec=30
上面記錄繼續在curSec並沒有改變重複lastSec。爲什麼第二次不更新,即使我每次循環都要更新它。
單獨問題:喬達時間http://joda-time.sourceforge.net/適合Android? – Joset