在我的应用程序中,我想将videoview显示为圆角。我已经尝试将videoview / surfaceview放置在具有设置为线性布局的圆角的linearlayout中,但它并不完美。我无法将圆角设置为videoview / surfaceview。我想将视图设置为下面的图像:
![enter image description here](https://istack.dev59.com/p5ASM.webp)
有人知道如何做到这一点吗?
在我的应用程序中,我想将videoview显示为圆角。我已经尝试将videoview / surfaceview放置在具有设置为线性布局的圆角的linearlayout中,但它并不完美。我无法将圆角设置为videoview / surfaceview。我想将视图设置为下面的图像:
有人知道如何做到这一点吗?
不确定为什么会有那么多错误的答案,因为解决方案实际上非常简单(只要您的min sdk > 21)。创建一个形状并将其放在视频之上是行不通的,因为您显然希望背景是透明的,以便看到它后面的视图。
我在这里找到了答案Android View Clipping。你只需要将视频视图放在框架布局中,向框架布局添加圆角背景,添加一个轮廓提供程序并将框架布局剪切到轮廓。
背景rounded_video_background
:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners android:radius="16dp" />
</shape>
框架布局和其中的视频视图:
<FrameLayout
android:id="@+id/video_view_container"
android:layout_width="90dp"
android:layout_height="120dp"
android:background="@drawable/rounded_video_background"
android:outlineProvider="background">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</FrameLayout>
最后一步是按照轮廓进行剪裁(在XML中没有找到方法,因此我通过编程实现):
video_view_container.clipToOutline = true
这对我很有效,
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="match_parent"
card_view:cardCornerRadius="25dp"
android:layout_height="wrap_content">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="215dp" />
</android.support.v7.widget.CardView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<VideoView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_240"
android:layout_margin="@dimen/dp_24"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_corner_video_bg" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="@dimen/dp_24"
android:color="@color/md_white_1000" />
<corners android:radius="@dimen/dp_24" />
</shape>
直接实现不可能,但是您可以通过在VideoView上绘制一个ImageView并设置一张透明的图片,然后在四个角落处设置一个圆形的纯色背景来实现这一点。 请参考:点击这里
这是 圆角 VideoView 的 XML 代码。
<androidx.cardview.widget.CardView
android:id="@+id/videoCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="20dp"
card_view:cardBackgroundColor="@color/white">
<VideoView
android:id="@+id/relativeVideo"
android:layout_width="match_parent"
android:layout_height="225dp"
android:paddingTop="-10dp"
android:paddingBottom="-10dp" />
</androidx.cardview.widget.CardView>
负填充非常重要,否则VideoView的高度将比cardview的顶部和底部的cornerRadius的一半小。您可以设置任何高度,但负填充应始终为cardCornerRadius的一半。图像中的紫色是视频预览,与xml无关。
祝您有美好的一天!
在drawable文件夹中创建一个名为shape_video.xml的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="6dp" />
</shape>
android:background="@drawable/shape_video"
android:background="@drawable/rounded.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFFFF" />
<corners android:radius="7dp" />
</shape>