【问题标题】:How to make a rounded Exoplayer PlayerView in Android如何在 Android 中制作圆形的 Exoplayer PlayerView
【发布时间】:2019-06-18 16:30:59
【问题描述】:

您好,我正在尝试创建一个圆形视频播放器,并且我正在使用 Exopaye2 库。我将 PLayerView 放在一个圆形的 FrameLayout 中,但不知道如何使 PlayerView 本身变成圆形。我试过this但它不起作用,我什至创建了一个rounded_shape_drawable并将其添加到Playeview的背景中,但它不起作用(基本上,为PlayeView设置背景根本不起作用)。

下面是我的简单布局文件:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="25dp"
    android:layout_weight="4"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <FrameLayout
        android:id="@+id/player_view_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:padding="10dp"
        android:background="@drawable/rounded_video_layout">

        <com.google.android.exoplayer2.ui.PlayerView

            android:id="@+id/exoplayer_player_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:fastforward_increment="@integer/exoplayer_playback_fastforward_increment_ms"
            app:resize_mode="fill"
            app:rewind_increment="@integer/exoplayer_playback_rewind_increment_ms"
            app:show_timeout="@integer/exoplayer_show_control_timeout_ms"
            app:use_artwork="true"
            app:use_controller="false">

        </com.google.android.exoplayer2.ui.PlayerView>

    </FrameLayout>

</LinearLayout>

以下是我当前的输出:

任何帮助将不胜感激

【问题讨论】:

  • 您可以制作一个带有黑色圆角和完全透明中间的可绘制资源,然后将其覆盖在视频播放器顶部的ImageView 内?

标签: android video background android-mediaplayer exoplayer


【解决方案1】:

也许你可以像这样使用 CardView 并且 PlayerView surface_type 必须是 texture_view

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp"
    app:cardElevation="0dp">

    <com.google.android.exoplayer2.ui.PlayerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:surface_type="texture_view" 
        app:use_controller="false" />

</android.support.v7.widget.CardView>

【讨论】:

    【解决方案2】:

    经过长时间的搜索,我无法仅在 xml 中制作圆角 PlayerView 或 ImageView(我认为不可能)。所以我决定用java来做。下面是代码:

    playerView.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 15);
                }
            });
    
            playerView.setClipToOutline(true);
    

    【讨论】:

    • 对我不起作用。播放器没有出现在我的情况下。删除此代码可以正常工作,但角不圆。
    • 需要surface_type="texture_view" 才能使此解决方案发挥作用
    【解决方案3】:

    在您的 FrameLayout 上,删除填充,然后在该视图上调用 clipToOutline(),例如

    player_view_layout.clipToOutline()
    

    默认情况下,视图背景用作视图的轮廓提供者,因此如果背景可绘制对象具有圆角,FrameLayout 中的所有内容将被裁剪以匹配。

    https://developer.android.com/training/material/shadows-clipping

    【讨论】:

    • 我以前试过这个,但它不起作用。问题是 PlayerView 相对于其父级没有改变
    • @Dav我在一个项目中有这个代码,它工作正常。
    猜你喜欢
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多