如何在 Android 中制作圆形的 Exoplayer PlayerView
How to make a rounded Exoplayer PlayerView in Android
您好,我正在尝试创建一个圆形视频播放器,我正在使用 Exoplaye2 库。我将 PLayerView 放在一个圆形的 FrameLayout 中,但不知道如何使 PlayerView 本身变圆。我试过 但它不起作用,我什至创建了一个 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>
下面是我当前的输出:
任何帮助将不胜感激
在您的 FrameLayout
上,移除填充,然后在该视图上调用 clipToOutline()
,例如
player_view_layout.clipToOutline()
默认情况下,视图背景用作视图的轮廓提供者,因此如果背景可绘制对象具有圆角,FrameLayout
中的所有内容都将被裁剪以匹配。
https://developer.android.com/training/material/shadows-clipping
经过长时间的搜索,我无法在 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);
也许你可以像这样使用 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>
您好,我正在尝试创建一个圆形视频播放器,我正在使用 Exoplaye2 库。我将 PLayerView 放在一个圆形的 FrameLayout 中,但不知道如何使 PlayerView 本身变圆。我试过
下面是我的简单布局文件:
<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>
下面是我当前的输出:
任何帮助将不胜感激
在您的 FrameLayout
上,移除填充,然后在该视图上调用 clipToOutline()
,例如
player_view_layout.clipToOutline()
默认情况下,视图背景用作视图的轮廓提供者,因此如果背景可绘制对象具有圆角,FrameLayout
中的所有内容都将被裁剪以匹配。
https://developer.android.com/training/material/shadows-clipping
经过长时间的搜索,我无法在 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);
也许你可以像这样使用 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>