[英]Camera flash blinking stalls app
我想在我的應用程序中添加一個新功能:我創建了一個進度條,並且每次進度更改時,閃光燈都會閃爍,其延遲等於進度值(以毫秒為單位)。
當我修改進度時,閃光燈開始閃爍,但是直到達到閃爍次數(20)時我什么也不能做。 即使閃光燈仍在閃爍,我也希望能夠修改延遲。
private void blink(int sleepMS) throws Exception{
//int sleepMS=(1/10)*50;
int flashCount=20;
getCamera();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
Thread thr = new Thread();
thr.start();
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
camera.stopPreview();
//camera.release();
}
private void flipFlash(){
if (isFlashOn) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
isFlashOn = false;
} else{
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
isFlashOn = true;
}
}
其中getCamera()
獲取相機參數。 還有SeekBar
偵聽器的代碼:
volumeControl = (SeekBar) findViewById(R.id.volume_bar);
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
blink(100 - progressChanged);
} catch (Exception e) {
}
}
});
因為您的for循環位於主線程上,所以它阻止了所有其他操作。 從您的代碼看來,您似乎打算這樣做:
Thread thr = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
}
});
thr.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.