
最近在做一个项目,在SurfaceView展示视频内容的时候,右上角有一个摄像机的图标,理想的效果图如下:

理想效果
代码如下:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="228dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="18dp">
<SurfaceView
android:id="@+id/sv_video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.cardview.widget.CardView>
<ImageView
android:layout_width="24dp"
android:layout_height="18.72dp"
android:layout_gravity="end"
android:layout_marginTop="13dp"
android:layout_marginEnd="18dp"
android:src="@mipmap/ic_main_video"/>
</FrameLayout>
然而实际上运行起来是这样的(右上角的ImageView就如同不存在一样):

实际效果
网上有很多解决方案,如代码设置setZOrderOnTop和setZOrderMediaOverlay等,但在这个项目里却不生效,后来发现一个简单的解决方案,就是用CardView把ImageView包裹起来即可,代码如下:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="228dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="18dp">
<SurfaceView
android:id="@+id/sv_video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/transparent"
android:background="@android:color/transparent">
<ImageView
android:layout_width="24dp"
android:layout_height="18.72dp"
android:layout_gravity="end"
android:layout_marginTop="13dp"
android:layout_marginEnd="18dp"
android:src="@mipmap/ic_main_video"/>
</androidx.cardview.widget.CardView>
</FrameLayout>
这是一个风趣幽默的博主,不定时分享技术和生活干货,赶紧关注一波吧~~