如何制作圆形的表面视图

How to make a rounded surfaceview

我正在尝试制作圆形表面视图。我搜索了很多,但找不到好的解决方案。我现在正在做的是,我将 SurfaceView 放入 FrameLayout 中,然后在其顶部放置另一个视图,使用 PNG 蒙版或形状 xml 可绘制。在这里

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#000"
        android:orientation="vertical"
        android:visibility="visible" >

        <FrameLayout
            android:id="@+id/videorecordview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight=".2" >

            <SurfaceView
            android:id="@+id/surfaceView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/rounded"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

但这不是一个好的解决方案,而且效果也不理想。我想将 surfaceview 自定义为圆形。任何帮助将非常感激。谢谢:)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="270"/>
</shape>

<SurfaceView
    android:background="@drawable/circle"
    android:id="@+id/surfaceView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

您无法更改 SurfaceView 的 Surface 的形状。

SurfaceView 有两部分,Surface 和 View。 View 部分像任何其他 View 一样工作。默认情况下,它只是充当透明 "hole",在布局中创建 window。应用程序将所有视图渲染到一个图层上。

表面部分是位于视图层后面的单独层(除非您明确更改表面的 Z 顺序),因此您只能在视图层的 "shows through" 透明区域看到它。您可以在 View 层上绘制以遮盖部分 Surface,但您不能更改 Surface 层本身的形状。图层是矩形的。

在许多情况下,可以使用 TextureView 代替 SurfaceView。 TextureView 提供了更大的灵活性,因为它由应用程序渲染到视图层,但效率可能低于 SurfaceView。

可以在 Android graphics architecture doc.

中找到更多信息

一点技巧。将表面视图置于卡片视图中。

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_normal" 
        app:cardCornerRadius="10dp"     
        app:cardPreventCornerOverlap="false">

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/margin_normal" />

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

不要忘记将此添加到您的 gradle 文件中以使用 CardView

compile 'com.android.support:cardview-v7:25.0.1'

卡片视图里面也有这两行

app:cardCornerRadius="10dp"     
app:cardPreventCornerOverlap="false"

干杯快乐编码

要达到你的要求,很容易做到。 你只需要使用下一个XML:

<androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="12dp">
        <SurfaceView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </androidx.cardview.widget.CardView>

要使用此 CardView,请将此依赖项添加到您的 build.gradle 应用程序:

实施'androidx.cardview:cardview:1.0.0'