本文介绍了需要帮助国米preT谷歌的应用程序摄像机更新计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数是code谷歌的相机应用程序的一部分。
它应该更新动态显示自记录开始经过的时间一个文本视图。但这个功能并没有一个循环,使它是如何做到的呢?请帮助。

私有静态最终诠释UPDATE_RECORD_TIME = 5;

 私人最终处理程序mHandler =新MainHandler(); 私有类MainHandler扩展了Handler {
        @覆盖
        公共无效的handleMessage(消息MSG){            开关(msg.what){                案例UPDATE_RECORD_TIME:{
                    updateRecordingTime();
                    打破;
                }                默认:
                    Log.v(TAG,未处理的消息:+ msg.what);
                    打破;
            }
        }
    }

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

  mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);//这个函数是更新记录时间私人无效updateRecordingTime(){如果(mMediaRecorderRecording!){返回; }
长今= SystemClock.uptimeMillis();
长三角洲=现在 - mRecordingStartTime;//达到最大持续时间开始前一分钟
//限制,我们会倒计时剩余时间来代替。
布尔countdownRemainingTime =(mMaxVideoDurationInMs!= 0
        &功放;&安培;三角洲> = mMaxVideoDurationInMs - 60000);长next_update_delay = 1000 - (增量%1000);
长秒;
如果(countdownRemainingTime){
    三角洲= Math.max(0,mMaxVideoDurationInMs - 三角);
    秒=(△+ 999)/ 1000;
}其他{
    秒=增量/ 1000; //向最接近
}长期分钟=秒/ 60;
长时间=分钟/ 60;
长remainderMinutes =分钟 - (小时* 60);
长remainderSeconds =秒 - (*分钟60);字符串secondsString = Long.toString(remainderSeconds);
如果(secondsString.length()2){
    secondsString =0+ secondsString;
}
字符串minutesString = Long.toString(remainderMinutes);
如果(minutesString.length()2){
    minutesString =0+ minutesString;
}
字符串文本= minutesString +:+ secondsString;
如果(小时大于0){
    字符串hoursString = Long.toString(小时);
    如果(hoursString.length()2){
        hoursString =0+ hoursString;
    }
    文= hoursString +:+文字;
}
mRecordingTimeView.setText(文本);如果(mRecordingTimeCountsDown!= countdownRemainingTime){    //避免设置在每次更新的颜色,只有做到这一点
    //当它需要改变。
    mRecordingTimeCountsDown = countdownRemainingTime;    INT颜色= getResources()。的getColor(countdownRemainingTime
            ? R.color.recording_time_remaining_text
            :R.color.recording_time_elapsed_text);    mRecordingTimeView.setTextColor(颜色);
}//各地的T-Mobile G1的限制工作:T-Mobile公司
//硬件的阻击器无法像素准确地扩展和夹在
//同时,和SurfaceFlinger的并不试图解决
//此限制。为了避免视觉腐败就要
改变任何时候//手动刷新整个表面观
//重叠的视图的内容。mVideo preview.invalidate();
mHandler.sendEmptyMessageDelayed(
        UPDATE_RECORD_TIME,next_update_delay);
}


解决方案

  mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME,next_update_delay);

本线发送 next_update_delay 的时间跨度之后的事件。我想,有 mHandler 地方和code处理该 UPDATE_RECORD_TIME 事件的声明。我打赌它会调用 updateRecordingTime ,从而在一段时间后发送消息。

这是一个循环的异步版本。该方法做它的工作,然后安排其自身一段时间后执行。

有但是,必须是初始调用为 updateRecordingTime 或将 sendMessageDelayed 方法之外的某处 updateRecordingTime 来开始启动循环。

The function below is part of the code for Google's Camera app.It is supposed to update a text view that displays dynamically the time elapsed since start of recording. But this function does not have a loop so how does it do it? Please help.

private static final int UPDATE_RECORD_TIME = 5;

private final Handler mHandler = new MainHandler();

 private class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case UPDATE_RECORD_TIME: {
                    updateRecordingTime();
                    break;
                }

                default:
                    Log.v(TAG, "Unhandled message: " + msg.what);
                    break;
            }
        }
    }  

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

mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);

//this function is to update the recording time

private void updateRecordingTime() {

if (!mMediaRecorderRecording) { return; } 
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); 
} 
解决方案
mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME, next_update_delay); 

This line sends an event after next_update_delay timespan. I suppose, there's a declaration of mHandler somewhere and code that handles the UPDATE_RECORD_TIME event. I'd bet it calls updateRecordingTime, which in turn sends the message after a period of time.

This is the asynchronous version of a loop. The method does its work and then schedules itself to be executed after some time.

There must, however, be an initial call to either updateRecordingTime or to the sendMessageDelayed method somewhere outside updateRecordingTime to initially start the loop.

这篇关于需要帮助国米preT谷歌的应用程序摄像机更新计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:40