如何在android中更改surfaceview的z顺序运行时

Tin*_*ino 7 android z-order surfaceview webrtc apprtcdemo

最近在研究android中的webrtc。

我想单击SurfaceView A 使其全屏,而另一个SurfaceView B 变小,或者单击 B。但我发现很难在运行时更改两个不同 SurfaceView 的 z 顺序。

这是我的布局

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">

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

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

</org.webrtc.PercentFrameLayout>

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

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/local_video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:keepScreenOn="true" />

</org.webrtc.PercentFrameLayout>
Run Code Online (Sandbox Code Playgroud)


PercentFrameLayout 是 ViewGroup 的子类,用于测量屏幕百分比。
我尝试在运行时使用 setZOrderMediaOverlay 但失败了。我发现SDK说

请注意,必须在将表面视图的包含窗口附加到窗口管理器之前设置此项。


那么这是否意味着我无法在运行时更改SurfaceView z 顺序?或者有什么办法可以破解它吗?

对不起,我的英语不好。



编辑1 SurfaceViewRenderPercentFrameLayout
都来自库,SurfaceViewRenderSurfaceView的子类。

Tin*_*ino 6

最后我尝试了很多次,自己解决了。

首先,我将SurfaceViewRenderA 和 B 更改为View.GONE,然后从父布局中删除 A 和 B,setZOrderMediaOverlayA false 和 B true 或 A true 和 B false。左边的脚步正好相反。

就像下面这样。

    mLocalRender.setVisibility(View.GONE);
    mRemoteRender.setVisibility(View.GONE);
    mLocalRenderLayout.removeView(mLocalRender);
    mRemoteRenderLayout.removeView(mRemoteRender);
    mLocalRender.setZOrderMediaOverlay(mLocalPreview);
    mRemoteRender.setZOrderMediaOverlay(!mLocalPreview);
    mLocalRenderLayout.addView(mLocalRender, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    mRemoteRenderLayout.addView(mRemoteRender, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    mLocalRender.setVisibility(View.VISIBLE);
    mRemoteRender.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)