繁体   English   中英

Android相机预览始终显示在FrameLayout中的其他SurfaceView上方

[英]Android camera preview is always shown above other SurfaceViews in FrameLayout

在Android 2.3应用程序中,我曾经支持两台相机:一个是内置相机,另一个是通过Wi-Fi在外部通过MJPG流式传输的。 内置摄像机必须一直在录制视频,并且用户必须能够在两者之间切换视图。 因此,每个摄像头都有一个专用的SurfaceView可以绘制。 由于最后一个添加到FrameLayout的子项具有较高的Z轴顺序,因此我以编程方式将所需的摄像机视图添加到最后一个FrameLayout中以使其可见。 我不能跳过添加内置摄像头的问题,因为没有预览显示就无法录制视频。

这种方案不再适用于Android 4:尽管先添加内置摄像机预览,但始终会显示内置摄像机预览。 我在视图上使用了“ setVisibility”方法,并注意到如果将其中一个视图设置为不可见(或消失),则根本不会显示任何视图。 ViewGroup的“ bringChildToFront”方法也无效。

那么,是否有一些解决方法可以使它在Android 4上运行? 而且我知道拥有多个SurfaceView被认为是不好的。 一些代码如下。

框架布局:

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

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

private void setCorrectView() {
    data.removeAllViews();

    List<Recorder> others = recorders.getOthers();
    for (Recorder other : others) {
        addToTheView(other);
    }

    addToTheView(recorders.getCurrent());
}

private void addToTheView(Recorder recorder) {
    View view = recorder.getView(this); // get recorder's surface view
    data.addView(view);
}

如果我使用XML中的FrameLayouts,则具有相同的效果:

<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>

无论我在哪里放置内置相机的表面视图-始终会显示出来。 看起来内置摄像头的预览现在总是在Android 4中居于首位。

对我自己回答。 为了完成这项工作,我启动相机之前将视图的可见性设置为View.VISIBLE, 之后将其设置为View.INVISIBLE。 链接可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM