1
在我的相機應用程序中,我有兩個曲面視圖,一個用於預覽相機,一個繪製稱爲tFrame的形狀(其類型是擴展surfaceView的TargetTarget)。 在開始它正常工作,但在應用程序不可見時onResume後,tFrame surfaceview不可見,我只查看預覽相機的人。surfaceview over camera surfaceview onresume後不可見
這裏是我的代碼
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
public Target_Frame tFrame;
private ColorView cViewActual;
private ColorView bestMatchCView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Function", "onCreate iniciado");
setContentView(R.layout.camera_layout);
tFrame=new Target_Frame(this);
cViewActual=(ColorView)findViewById(R.id.colorViewActual);
bestMatchCView=(ColorView)findViewById(R.id.colorViewBestMatch);
mCamera=getCameraInstance();
mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,tFrame,colorNametextView);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER;
tFrame.setLayoutParams(params);
preview.addView(tFrame);//the first to put is the one on the bottom of the view
preview.addView(mPreview);
}
/** Check if this device has a camera only if not specified in the manifest */
public boolean checkCameraHardware(Context context) {
Log.d("Function", "checkCameraHardware iniciado");
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Log.d("Function", "getCameraInstance iniciado");
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/**Check if the device has flash*/
public boolean checkFlash(Context context){
Log.d("Function", "checkFlash iniciado");
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
//the device has flash
return true;
}else{
//no flash
return false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Function", "onDestroy CameraActivity iniciado");
releaseCamera();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Function", "onPause CameraActivity iniciado");
releaseCamera();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Function", "onResume CameraActivity iniciado");
//Test if i have to put all this code like in onCreate
if(mCamera!=null){
return;
}
mCamera=getCameraInstance();
//if(mPreview!=null){
// return;
//}
mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,tFrame,colorNametextView);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private void releaseCamera(){
Log.d("Function", "releaseCamera iniciado");
if (mCamera != null){
mCamera.setPreviewCallback(null);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}