Android 相机预览需要一些时间来重新绘制方向更改

Android 相机预览需要一些时间来重新绘制方向更改,android,camera,screen-orientation,preview,Android,Camera,Screen Orientation,Preview,我正在开发一款定制的Android摄像头应用程序。它与预览屏幕配合良好,在纵向和横向模式下都与屏幕大小非常匹配。但是,当更改手机方向时,预览重画会有明显的延迟。在论坛上搜索时,我发现其他人在处理手机定位变化时也遇到了类似的问题。为了避免活动被破坏和重新创建,以及在这样的更改过程中相机被释放和打开,我在AndroidManifest.xml中添加了以下行: android:configChanges="orientation|screenSize"> 通过一些调试和测试,我

我正在开发一款定制的Android摄像头应用程序。它与预览屏幕配合良好,在纵向和横向模式下都与屏幕大小非常匹配。但是,当更改手机方向时,预览重画会有明显的延迟。在论坛上搜索时,我发现其他人在处理手机定位变化时也遇到了类似的问题。为了避免活动被破坏和重新创建,以及在这样的更改过程中相机被释放和打开,我在AndroidManifest.xml中添加了以下行:

        android:configChanges="orientation|screenSize">
通过一些调试和测试,我可以确认在方向更改时不再销毁/创建活动。但是,即使添加了此选项,预览从纵向重画到横向重画时也会有明显的延迟,反之亦然。下面是Activity类中onConfiguration Changed方法的代码:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mPreview.setCameraDisplayOrientation(this, 1, mCamera);
}
在SurfaceView类中:

public void setCameraDisplayOrientation(Activity activity,
        int cameraId, android.hardware.Camera camera) {

    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result = (info.orientation + degrees) % 360;
    result = (360 - result) % 360;  // compensate the mirror
    camera.setDisplayOrientation(result);
}
在surfaceChanged中,我有:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    initialisePreview(w, h); // gets best preview size and sets camera parameters
    mCamera.startPreview();
    }

上面的
setCameraRadisPlayOrientation
方法没有什么新的,它是从Android开发者网站改编的。我不知道还有什么会导致重新绘制的延迟。如果有人有任何想法,请提出建议。多谢各位

我没有如上所述处理屏幕方向的变化,而是在
onCreate()
期间使用以下方法使活动忽略方向传感器确定的方向变化:

setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u NOSENSOR)

通过此更改,当手机方向更改时,预览将无缝继续。这听起来有悖常理,但它解决了我的问题。希望它能帮助别人