我对Android很新,我试图按下按钮时闪光灯闪烁。我正在使用的代码是简单地打开和关闭手电筒。我正在尝试添加另一个按钮,使手电筒以某种模式闪烁。是否有可能像我们在模式中振动一样设置模式参数
我的代码如下:
public class MainActivity extends Activity {
private InterstitialAd interstitial;
private Camera camera;
ImageButton flashLightSwitchImage;
private boolean isFlashlightOn;
Parameters params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashLightSwitchImage = (ImageButton) findViewById(R.id.flashlight_switch);
boolean isCameraFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isCameraFlash) {
showCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
flashLightSwitchImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (isFlashlightOn) {
setFlashLightOff();
} else {
setFlashLightOn();
}
}
});
}
private void showCameraAlert() {
new AlertDialog.Builder(this)
.setTitle("Error loadin Flash!")
.setMessage("Flash Not Available in this Device")
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
finish();
}
}).setIcon(android.R.drawable.ic_dialog_alert).show();
}}}}
This code right here is working fine but it simply switches on and off the flashlight.
I want to introduce another button which makes the flash light blink in a pattern.
答案 0 :(得分:0)
尝试使用此代码闪烁闪光灯。您可以编辑它以支持您所需的模式
String myString = "01010101010101";
long blinkDelay 50; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
params.setFlashMode(Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
} try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
} }
答案 1 :(得分:0)
在按钮的监听器上使用此代码
getCamera();
Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
String myString = "01010101010010101010101";
long blinkDelay =500;
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
//params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
turnOnFlash();
} else {
// params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
turnOffFlash();
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
并在create method
之外定义此代码 private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
// Log.d("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
/*
* Turning On flash
*/
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
// playSound();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
// toggleButtonImage();
}
}
/*
* Turning Off flash
*/
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
//playSound();
params = camera.getParameters();
params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
// toggleButtonImage();
}}