我是Android开发和学习Android应用程序开发的新手。我为Android设备创建了一个非常基本和简单的Flashlight。我正在面对这个问题,当我运行应用程序需要一些时间来运行,如果我按下开启闪光灯需要一些时间(半秒或更短但需要一些时间),我没有使用等待( )我的应用程序中的方法。如何快速运行它,如用户点击闪光灯开启或关闭?
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
private Camera.Parameters params;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.switch_btn);
//Check that Device has supports flash or not
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash){
//If device does not supports Flash
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your current device does not support to Little Flashy! ops");
alert.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Close application
finish();
}
});
alert.show();
return;
}
//Get the Camera
getCamera();
//Display button image
toggleButtonImage();
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else
{
turnOnFlash();
}
}
});
}
private void toggleButtonImage() {
if (isFlashOn){
imageButton.setImageResource(R.drawable.btn_switch_on);}
else {imageButton.setImageResource(R.drawable.btn_switch_off);}
}
private void getCamera() {
if (camera == null){
try{
camera = camera.open();
params = camera.getParameters();
}catch (RuntimeException e){
Log.d("Camera 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(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(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
private void playSound() {
if (isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);}
else {
mp= MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
//turn off flash when on Pause called
turnOffFlash();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
if (hasFlash) turnOnFlash();
}
@Override
protected void onStart() {
super.onStart();
getCamera();
}
@Override
protected void onStop() {
super.onStop();
if (camera != null){
camera.release();
camera = null;
}
}
}
答案 0 :(得分:2)
在开启和关闭闪光灯之前,请调用playSound
方法,该方法使用MediaPlayer
。这种方法很慢,导致你的延迟。首先尝试将其删除(通过评论)并查看差异。接下来,您可以尝试从线程运行它。
答案 1 :(得分:1)
是的,您可以忽略当前时间的声音功能。或者,如果你真的想要这个功能而不是通过线程使用它,那么在用户打开或关闭闪光灯时,你的应用程序就不会出现延迟或延迟。