我正在使用 SurfaceView 来保存 Camera 对象,并将视图包装在 FrameLayout 中。我还在持有相机视图的同一活动中添加了一个输入文本框。当我点击它时,软键盘打开并覆盖了设备屏幕的一半(Galaxy S2),在这种情况下我能够看到一个非黑屏。我想知道为什么打开软键盘时,相机开始工作,而当我关闭软键盘时,它又变黑了
public class CameraActivity extends Activity {
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
private EditText editTextServerIP;
Button myButton;
Button btnUpload;
SurfaceHolder surfaceHolder;
boolean recording;
Button btnConnectVideo;
EditText savePath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//policy to prevent exception when stop record
//android.os.NetworkOnMainThreadException
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
/////////////////////////////////////////////////////////////////////////////////////////////
recording = false;
setContentView(R.layout.activity_camera);
// Get Camera for preview
myCamera = getCameraInstance();
if (myCamera == null) {
Toast.makeText(CameraActivity.this, "Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout) findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button) findViewById(R.id.btnRecord);
myButton.setOnClickListener(myButtonOnClickListener);
btnUpload = (Button) findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(btnUploadOnClickListener);
editTextServerIP = (EditText) findViewById(R.id.editTextServerIP);
btnConnectVideo = (Button) findViewById(R.id.btnConnectVideo);
btnConnectVideo.setOnClickListener(askServerToConvertAndConnectAllVideos);
savePath = (EditText) findViewById(R.id.editTextPath);
}
公共类 MyCameraSurfaceView 扩展 SurfaceView 实现 SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format,
int weight, int height) {
// If your preview can change or rotate, take care of those events
// here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
}
}