Android 两个surfaceview叠加的问题

    最近在做安卓视频通话,用到webrtc,要求跟对方通话的时候右上角小窗口展示本地视频,底层展示远程视频,想到的方法是在Framelayout里面嵌套两个org.webrtc.SurfaceViewRenderer(继承自SurfaceView),把远程SurfaceView放在下面,把本地SurfaceView放在上面,如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/remoteVideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/localVideoView"
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:layout_gravity="top|right"
        android:fitsSystemWindows="true" />
</FrameLayout>

但是结果是在华为mate9上(Android O)显示正常,在海信手机(Android L)上显示异常,查看了相关的文档 SurfaceView

The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.

 surface是纵深排序(Z-ordered)的,这表明它总在自己所在窗口的后面。surfaceview提供了一个可见区域,只有在这个可见区域内 的surface部分内容才可见,可见区域外的部分不可见。surface的排版显示受到视图层级关系的影响,它的兄弟视图结点会在顶端显示。这意味者 surface的内容会被它的兄弟视图遮挡,这一特性可以用来放置遮盖物(overlays)(例如,文本和按钮等控件)。注意,如果surface上面 有透明控件,那么它的每次变化都会引起框架重新计算它和顶层控件的透明效果,这会影响性能。

 两个SurfaceView创建的先后顺序不受Framelayout影响,是由重新计算的Surface的创建顺序决定的,并且,SurfaceView提供了接口来对其Z-ordered进行排序。

public void setZOrderOnTop (boolean onTop)

Added in API level 5

Control whether the surface view's surface is placed on top of its window. Normally it is placed behind the window, to allow it to (for the most part) appear to composite with the views in the hierarchy. By setting this, you cause it to be placed above the window. This means that none of the contents of the window this SurfaceView is in will be visible on top of its surface.

Note that this must be set before the surface view's containing window is attached to the window manager.

Calling this overrides any previous call to setZOrderMediaOverlay(boolean).

 因此,我们可以讲本地surfaceview设置为setZOrderOnTop (true),那么就可以达到将本地SurfaceView展示在远程SurfaceView上层的目的了。

猜你喜欢

转载自blog.csdn.net/email_jade/article/details/82895335