如何在安卓中创建圆角的VideoView?

3

在我的应用程序中,我想将videoview显示为圆角。我已经尝试将videoview / surfaceview放置在具有设置为线性布局的圆角的linearlayout中,但它并不完美。我无法将圆角设置为videoview / surfaceview。我想将视图设置为下面的图像:

enter image description here

有人知道如何做到这一点吗?


你不能给它一个形状吗?你尝试了什么? - Chintan Rathod
我尝试了下面的代码: - Mihir Shah
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@android:color/transparent" android:endColor="@android:color/transparent" android:angle="270"/> <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp"/> <stroke android:color="#000000" android:width="1dp" /> </shape> - Mihir Shah
只保留“corners”,移除所有其他标签,这样可以得到所需的输出。 - Chintan Rathod
也许这个链接可以帮助你 http://tips.androidhive.info/2013/09/android-layout-rounded-corner-border/,还有这个链接 https://dev59.com/hHLYa4cB1Zd3GeqPZ6HN - Zar E Ahmer
这种方法在我的情况下解决了问题:https://dev59.com/s2035IYBdhLWcg3wMM4G - Aaron
9个回答

7

不确定为什么会有那么多错误的答案,因为解决方案实际上非常简单(只要您的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

5

这对我很有效,

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

3
卡片视图很糟糕,因为内部视频视图没有圆角半径,会出现方形。 - CodingTT

1
你可以使用FramLayout和XML可绘制来制作它。
FramLayout
        <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 Drawable(可翻译为“XML可绘制对象”)
<?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>

0
很简单, 1. 创建一个带有圆角的可绘制对象,按照 @sunil kumar 的建议进行操作 2. 将该可绘制对象设置为您布局的背景 3. 在使用该布局时,设置 layout(your layout item name).clipToOutline = true

0
你可以尝试将不同的视图层叠在一起,以创建所需的圆角效果。尝试在每个角落的VideoView上放置四个ImageView,以实现所需的圆角效果。我曾经成功地使用RelativeLayout来完成这个任务,但你也可以尝试使用FrameLayout来组合这些视图。

0

直接实现不可能,但是您可以通过在VideoView上绘制一个ImageView并设置一张透明的图片,然后在四个角落处设置一个圆形的纯色背景来实现这一点。 请参考:点击这里


0

这是 圆角 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无关。

祝您有美好的一天!

enter image description here


-3

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

根据您的需求更改半径,并在您的videoview的background属性中设置。
android:background="@drawable/shape_video"

-3
将rounded.xml放入drawable文件夹中,并在视频的FrameLayout上设置背景,如下所示: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>

我使用了你上面提供的代码,将其设置为帧布局背景,但是它没有起作用。这只是针对VideoView/SurfaceView存在问题,而不是其他任何组件。 - Mihir Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接