Camera オブジェクトを保持するために SurfaceView を使用しており、ビューを 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);
}
public class MyCameraSurfaceView extends SurfaceView implement 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) {
}
}