2010-07-06 49 views
0

以下功能是Google相機應用代碼的一部分。我不明白粗體陳述的邏輯。請幫忙。需要幫助來解釋Google的Android相機應用代碼

int seconds = intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT,0); mMaxVideoDurationInMs = 1000 *秒;

mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);

//這個函數是更新記錄時間

私人無效updateRecordingTime(){ 如果(mMediaRecorderRecording!){ 回報; } long now = SystemClock.uptimeMillis(); long delta = now - mRecordingStartTime;

// Starting a minute before reaching the max duration 
    // limit, we'll countdown the remaining time instead. 
    boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 
      && delta >= mMaxVideoDurationInMs - 60000); 

    long next_update_delay = 1000 - (delta % 1000); 
    long seconds; 
    if (countdownRemainingTime) { 
     **delta = Math.max(0, mMaxVideoDurationInMs - delta); 
     seconds = (delta + 999)/1000;** 
    } else { 
     **seconds = delta/1000; // round to nearest** 
    } 

    long minutes = seconds/60; 
    long hours = minutes/60; 
    long remainderMinutes = minutes - (hours * 60); 
    long remainderSeconds = seconds - (minutes * 60); 

    String secondsString = Long.toString(remainderSeconds); 
    if (secondsString.length() < 2) { 
     secondsString = "0" + secondsString; 
    } 
    String minutesString = Long.toString(remainderMinutes); 
    if (minutesString.length() < 2) { 
     minutesString = "0" + minutesString; 
    } 
    String text = minutesString + ":" + secondsString; 
    if (hours > 0) { 
     String hoursString = Long.toString(hours); 
     if (hoursString.length() < 2) { 
      hoursString = "0" + hoursString; 
     } 
     text = hoursString + ":" + text; 
    } 
    mRecordingTimeView.setText(text); 

    if (mRecordingTimeCountsDown != countdownRemainingTime) { 
     // Avoid setting the color on every update, do it only 
     // when it needs changing. 
     mRecordingTimeCountsDown = countdownRemainingTime; 

     int color = getResources().getColor(countdownRemainingTime 
       ? R.color.recording_time_remaining_text 
       : R.color.recording_time_elapsed_text); 

     mRecordingTimeView.setTextColor(color); 
    } 

    // Work around a limitation of the T-Mobile G1: The T-Mobile 
    // hardware blitter can't pixel-accurately scale and clip at the 
    // same time, and the SurfaceFlinger doesn't attempt to work around 
    // this limitation. In order to avoid visual corruption we must 
    // manually refresh the entire surface view when changing any 
    // overlapping view's contents. 
    mVideoPreview.invalidate(); 
    mHandler.sendEmptyMessageDelayed(
      UPDATE_RECORD_TIME, next_update_delay); 
} 

回答

1

你是指這些行嗎?

1.  delta = Math.max(0, mMaxVideoDurationInMs - delta); 
2.  seconds = (delta + 999)/1000; 

3.  seconds = delta/1000; // round to nearest 

這很難說準確,因爲沒有什麼是你的問題大膽(但**之前和之後的這些行讓我覺得這是你指什麼)。

delta以毫秒爲單位,其中有1000秒。

1行:確保0被示出,而不是負數

2行:舍入到最接近的第二。由於seconds是一個整數,因此從1001(1s 1ms)到1999(1s,999ms)的任何值delta將返回爲seconds = 2

3號線:只需滴毫秒部分,所以delta = 1回報0,但這樣做delta = 999


Delta意味着「變化」,就像時間變化量一樣。

兩者幾乎相同,但是當代碼顯示剩餘秒數時,它選擇四捨五入以便例如剩餘1.5秒(1500毫秒)顯示爲1秒。

當它計數秒數後,它向下舍入,例如, 1.5s(1500ms)已過時顯示爲1秒。

由於seconds是一個int,所以任何浮點數都會將小數部分切掉。所以:

1500/1000 = 1.500 
(int)1.500 = 1 

當您添加999你,而不是有:

(1500 + 999)/1000 = 2499/1000 = 2.499 
(int)2.499 = 2 
+0

是這些都是我指to.Thank你行。 但是,我真的不明白'三角洲'。第2行和第3行有什麼區別?在第2行,ms正在下降不是嗎?第3行的需求是什麼? – Namratha 2010-07-07 03:59:50

+0

我更新了答案(因爲答案部分的格式更容易)。這有幫助嗎? – mbafford 2010-07-07 11:59:31

+0

布爾countdownRemainingTime =(!mMaxVideoDurationInMs = 0 && 增量> = mMaxVideoDurationInMs - 60000); 你能解釋一下這條線嗎? – Namratha 2010-07-08 06:18:04