为什么使用Android SurfaceView会出现黑屏?

时间:2012-11-12 22:34:09

标签: android android-layout surfaceview

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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