0

我正在使用 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) {
        }
    }
4

2 回答 2

0

问题出现在 Android 4.0.4 内核(自定义 ROM)中,问题和修复可以在下面找到:
XDA - android ROM 4.0.4 camera fix

于 2012-11-14T22:03:33.743 回答
0

你在 SurfaceHolder.Callback 中做什么?当软键盘打开时,它应该调整 SurfaceView 的大小,这会触发 surfaceChanged 回调。如果您查看此处的示例代码,您需要在 Surface 配置更改时停止预览并使用新的 SurfaceHolder 重新启动它,因为它可能与您开始使用的尺寸不同。

于 2012-11-14T23:41:14.227 回答