我有一个手电筒代码,它使用两个按钮(on_btn 和 off_btn)来打开和关闭手电筒。
如何将它们关联到一个按钮中?
很菜鸟可以给点详细的建议吗?
下面的代码是从答案中找到的:它适用于我的手机。但是在模拟器上单击按钮时它会崩溃。这是猫日志https://dl.dropbox.com/u/15065300/logcat1.png
第74行是:Parameters params = mCamera.getParameters();
有人可以提出任何建议吗?
public class FlashLight extends Activity {
private final static String LOG_TAG = "FlashLight";
private Button mOnBtn;
private Camera mCamera;
private boolean isActive;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//this will be inside your onCreate...
mOnBtn = (Button) findViewById(R.id.on_btn);
mOnBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
flipSwitch();
processClick();
}
});
}
//these will be outside your onCreate
public void flipSwitch() {
isActive = !isActive;
}
@Override
protected void onResume() {
super.onResume();
try{
mCamera = Camera.open();
mCamera.startPreview();
Toast.makeText(getApplicationContext(),"Camera is present", Toast.LENGTH_LONG).show();
} catch( Exception e ){
Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
}
}
@Override
protected void onPause() {
if( mCamera != null ){
mCamera.release();
mCamera = null;
}
super.onPause();
}
public void processClick() {
if(isActive) {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "torch");
mCamera.setParameters( params );
mCamera.startPreview();
}
else {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "off");
mCamera.setParameters( params );
mCamera.stopPreview();
}
}
}