如何让手电筒在Android中闪烁

时间:2017-06-30 17:43:47

标签: java android performance

在退出线程后的代码中,闪光灯仍然亮着。我无法理解逻辑错误。如何在线程末端关闭闪光灯?

Thread thread = new Thread((new Runnable() {
    @Override
    public void run() {
        SystemClock.sleep(200);
        int led = 1;
        while (blink_enabler) { 
           if (led == 1) {
                params = cam.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(params);
                cam.startPreview();
                led = 0;
            } else {
                params = cam.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                cam.setParameters(params);
                cam.stopPreview();
                led = 1;
            }
            SystemClock.sleep(1000);
        }
        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        cam.stopPreview();
    }
}));

1 个答案:

答案 0 :(得分:0)

在while循环之后,在stopPreview()

之前设置摄像机的参数
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();