Android摄像头预览始终显示在FrameLayout中其他SurfaceView的上方

Android摄像头预览始终显示在FrameLayout中其他SurfaceView的上方,android,android-camera,Android,Android Camera,在Android 2.3应用程序中,我曾经支持两个摄像头:一个内置摄像头和一个使用Wi-Fi传输的外置MJPG摄像头。内置摄像头必须一直在录制视频,用户必须能够在这些摄像头之间切换视图。因此,每个摄像头都有一个专用的SurfaceView可供绘制。由于添加到FrameLayout的最后一个子项具有更高的Z顺序,因此我通过编程将所需的摄影机视图添加到FrameLayout,以使其可见。我不能跳过添加内置摄像头,因为它无法在没有预览显示的情况下录制视频 该方案不再适用于Android 4:内置摄像头

在Android 2.3应用程序中,我曾经支持两个摄像头:一个内置摄像头和一个使用Wi-Fi传输的外置MJPG摄像头。内置摄像头必须一直在录制视频,用户必须能够在这些摄像头之间切换视图。因此,每个摄像头都有一个专用的SurfaceView可供绘制。由于添加到FrameLayout的最后一个子项具有更高的Z顺序,因此我通过编程将所需的摄影机视图添加到FrameLayout,以使其可见。我不能跳过添加内置摄像头,因为它无法在没有预览显示的情况下录制视频

该方案不再适用于Android 4:内置摄像头预览始终显示,无论是先添加还是后添加。我在视图上使用了“setVisibility”方法,并注意到如果其中一个视图被设置为不可见(或消失),那么它们中的任何一个都不会显示。ViewGroup的“bringChildToFront”方法也没有效果

那么,在安卓4上实现这一点有什么解决办法吗?我知道拥有多个SurfaceView被认为是不好的。下面是一些代码

框架布局:

<FrameLayout android:id="@id/data"
             android:layout_width="fill_parent" android:layout_height="fill_parent"/>

填充布局的代码(不再适用于Android 4):

private void setCorrectView(){
data.removeallview();
List others=记录器。getOthers();
用于(其他:其他){
添加到视图(其他);
}
添加到视图(recorders.getCurrent());
}
私有void添加到视图(记录器){
View View=recorder.getView(this);//获取记录器的表面视图
data.addView(视图);
}
如果使用XML中的框架布局,效果相同:

<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
         xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout
        android:id="@id/data"
        android:layout_width="fill_parent" android:layout_height="fill_parent"/>

    <FrameLayout
        android:id="@+id/data_invisible"
        android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</FrameLayout>


无论我把内置相机的表面视图放在哪里,它总是显示出来。在安卓4中,内部摄像头的预览功能现在似乎一直处于领先地位。

回答我自己的问题。为了实现这一点,我在启动相机之前将视图的可见性设置为View.VISIBLE,在启动相机之后将其设置为View.VISIBLE。链接可能会有所帮助

<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
         xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout
        android:id="@id/data"
        android:layout_width="fill_parent" android:layout_height="fill_parent"/>

    <FrameLayout
        android:id="@+id/data_invisible"
        android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</FrameLayout>