如何根据seekBar

时间:2016-06-18 23:51:25

标签: android multithreading flash android-camera android-seekbar

我正在制作像this这样的应用。一切都很好,但有一点问题。我想用进度条的值改变Flash Light的Flash(ON / OFF)持续时间。意味着线程应按持续时间 progressValue 进行休眠。

当调用onProgressChanged方法时,我希望调用方法flash_toggle_times(final int delay)在每次更改progressValue时更改sleep(delay)的值。

public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {flash_toggle_times(progressValue);}

以下是flash_toggle_times(SeekBar seekbar,int progressValue,Boolean fromUser)的代码

public void flash_toggle_times(final int delay) {
    mCamera = Camera.open();     
    final Parameters p = mCamera.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);


    Thread t = new Thread() {
        public void run() {
            try {
                // Switch on the cam for app's life
                if (mCamera == null) {
                    // Turn on Cam
                    mCamera = Camera.open();
                    try {
                        mCamera.setPreviewDisplay(null);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mCamera.startPreview();
                }



                while(positionON )
                {
                 if(delay > 0){
                    toggleFlashLight();
                    sleep(delay);
                }
                }

                if (mCamera != null) {
                    mCamera.stopPreview();
                    mCamera.release();
                    mCamera = null;
                }
            } catch (Exception e){ 
                e.printStackTrace(); 
            }
        }
    };

    t.start();
}

public void turnOn(){
     if (mCamera != null) {
            // Turn on LED
            mParams = mCamera.getParameters();
            mParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(mParams);
     }
     lightON = true;
}
public void turnOff(){
    // Turn off flashlight
    if (mCamera != null) {
        mParams = mCamera.getParameters();
        if (mParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
            mParams.setFlashMode(Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(mParams);
        }
    }
    lightON = false;
}
private void toggleFlashLight() {
    if (!lightON) {
        turnOn();
    } else {
        turnOff();
    }
}

}

0 个答案:

没有答案