You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何为VideoView设置圆角?

解决VideoView圆角不生效的问题

核心修复方案

直接修改FrameLayout的两个属性即可解决视频超出圆角的问题:

  • 将android:clipToChildren="false"改为android:clipToChildren="true"
  • 添加android:clipToOutline="true"属性,让父布局按背景轮廓裁剪子View

修改后的完整布局代码:

<FrameLayout
    android:id="@+id/media_video"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@drawable/video_view_bg"
    android:clipToChildren="true"
    android:clipToOutline="true"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <VideoView
        android:id="@+id/media_video_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

原因说明

之前设置android:clipToChildren="false"会允许VideoView超出父布局边界显示,导致视频内容突破圆角。而android:clipToOutline="true"会让FrameLayout根据自身背景的shape圆角轮廓,自动裁剪内部的子View,这样视频就会被限制在圆角范围内了。


备选方案:改用TextureView(兼容性更强)

如果部分系统版本中SurfaceView(VideoView的底层实现)的裁剪效果不稳定,可以换成TextureView,它的裁剪表现更可靠:

  1. 替换布局中的VideoView:
<FrameLayout
    android:id="@+id/media_video"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@drawable/video_view_bg"
    android:clipToChildren="true"
    android:clipToOutline="true"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextureView
        android:id="@+id/media_texture_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>
  1. 代码中用MediaPlayer配合TextureView播放视频:
TextureView textureView = findViewById(R.id.media_texture_item);
MediaPlayer mediaPlayer = new MediaPlayer();
try {
    mediaPlayer.setDataSource(this, Uri.parse("你的视频地址"));
    mediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
    mediaPlayer.prepare();
    mediaPlayer.start();
} catch (IOException e) {
    e.printStackTrace();
}

内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:52:45