【问题标题】:why using Android SurfaceView gives black screen?为什么使用 Android SurfaceView 会出现黑屏?
【发布时间】:2012-11-01 09:45:17
【问题描述】:

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

【问题讨论】:

    标签: android android-layout surfaceview


    【解决方案1】:

    问题是在Android 4.0.4内核(自定义ROM)中发现的,问题和修复方法如下:
    XDA - android ROM 4.0.4 camera fix

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        相关资源
        最近更新 更多