带闪光灯的手电筒:闪光灯不会关闭

时间:2017-01-17 22:24:01

标签: java android button flashlight

我正在尝试创建一个具有频闪效果的手电筒应用。我的目标是我可以通过开关Btn打开/关闭手电筒,并使用搜索栏增加选通频率。如果搜索条值为0且开关Btn为ON,则手电筒应为ON(没有任何选通)。但是如果seekbar值为0,它现在仍然闪烁(选通效果ON)。你知道我怎么能改变它吗? 以下是一些我认为很重要的代码(我真的无法找到能够保持stobe效果的部分:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mySwitch = (Switch) findViewById(R.id.mySwitch);

    //listener to check changes in state
    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            turnOnOff(isChecked);
        }
    });
    skBar = (SeekBar) findViewById(R.id.seekBar);
    skBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            freq = progress;
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
}
@Override
protected void onResume() {
    super.onResume();
    try {
        cam = Camera.open();
        camParams = cam.getParameters();
        cam.startPreview();
        hasCam = true;
    }catch(Exception e){
    }
}
private void turnOnOff(boolean on){
    if(on) {
        if(freq != 0){
            sr = new StroboRunner();

            t = new Thread(sr);
            t.start();
            return;
        }else{
            camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }
    }else if(!on){
        if(t != null){
            sr.stopRunning = true;
            t =null;
            return;
        }else{
            camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
    }
    cam.setParameters(camParams);
    cam.startPreview();
}
private class StroboRunner implements Runnable {

    boolean stopRunning = false;
    @Override
    public void run() {
        Camera.Parameters paramsOn = cam.getParameters();
        Camera.Parameters paramsOff = camParams;
        paramsOn.setFlashMode(Parameters.FLASH_MODE_TORCH);
        paramsOff.setFlashMode(Parameters.FLASH_MODE_OFF);
        try {
            while (!stopRunning) {
                cam.setParameters(paramsOn);
                cam.startPreview();
                Thread.sleep(100 - freq);
                cam.setParameters(paramsOff);
                cam.startPreview();
                Thread.sleep(100 - freq);

            }
        }catch(Exception e){
            }
        }
    }

}

0 个答案:

没有答案