需要帮助来解释谷歌的Android相机应用程序代码

时间:2010-07-06 16:56:25

标签: android

以下功能是Google相机应用程序代码的一部分。我不明白粗言秽语的逻辑。请帮忙。

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

mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);

//此功能用于更新录制时间

private void updateRecordingTime(){         if(!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 个答案:

答案 0 :(得分: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是整数,因此从delta(1s 1ms)到1001(1s,999ms)的1999的任何值都将返回为seconds = 2。< / p>

第3行:只需删除ms部分,delta = 1返回0delta = 999也是如此。


Delta意味着“变化”与时间变化量相同。

这两个几乎相同,但是当代码显示剩余秒数时,它选择向上舍入以便例如剩余1.5秒(1500毫秒)显示为1秒。

当计算秒数时,它会向下舍入,以便例如经过1.5秒(1500毫秒)显示为1秒。

由于秒是一个int,任何浮点数都会将小数部分切掉。所以:

1500 / 1000 = 1.500
(int)1.500 = 1

当您添加999时,您改为:

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