我的按钮有问题。根据条件,按钮应该调用两种方法中的任何一种。我面临的问题是当方法turnOnFlash()被调用一次时,程序仍然在其中#39 ; s状态和按钮只是停止响应。我有一些方法可以使这项工作
按钮的代码是
btnSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
并且方法turnOnFlash的代码是
public void turnOnFlash() {
isFlashOn = true;
// ThreadDemo T1 = new ThreadDemo();
// T1.setPriority(10);
// start new thread to flash
// T1.start();
// T1.run();
toggleButtonImage();
new Thread(new Runnable() {
public void run() {
// changing button/switch image
//toggleButtonImage();
//ImageButton btnSwitch1;
Visualizer mVisualizer = new Visualizer(0);
mVisualizer.setEnabled(false);
int capRate = Visualizer.getMaxCaptureRate();
int capSize = Visualizer.getCaptureSizeRange()[1];
mVisualizer.setCaptureSize(capSize);
Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
Camera cam = Camera.open();
Parameters params;
int length = bytes.length;
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
for (int i = 0; i < length; i++) {
Log.w("Morphyre", "Found Nonzero sample " + bytes[i]);
if (bytes[i] > 90 || bytes[i] <= -90) {
Log.d("hehe", "pepe");
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
} else {
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
//Log.d("check", "control");
}
}
if (cam != null) {
cam.stopPreview();
cam.release();
cam = null;
}
bytes = null;
System.gc();
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate) {
}
};
int status = mVisualizer.setDataCaptureListener(captureListener,
capRate, true/* wave */, false/* no fft needed */);
mVisualizer.setEnabled(true); }
}).start();
}