簡體   English   中英

如何制作圓形表面視圖

[英]How to make a rounded surfaceview

我正在嘗試制作圓形的表面視圖。 我已經搜索了很多,但我找不到一個好的解決方案。 我現在正在做的是,我已將 SurfaceView 放入 FrameLayout,然后在其頂部放置另一個 View,要么帶有 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>

但這不是一個好的解決方案,它也不能完美地工作。 我想將表面視圖自定義為圓形。 任何幫助將不勝感激。 謝謝:)

一個小黑客。 將您的表面視圖放在卡片視圖中。

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

干杯快樂編碼

您無法更改 SurfaceView 的 Surface 的形狀。

SurfaceView 有兩個部分,Surface 和 View。 View 部分的工作方式與任何其他 View 一樣。 默認情況下,它只是作為一個透明的“洞”,在布局中創建一個窗口。 所有視圖都由應用程序渲染到單個圖層上。

Surface 部分是位於 View 層后面的一個單獨層(除非您明確更改 Surface 的 Z 順序),因此您只能在它“通過”View 層的透明區域“顯示”的地方看到它。 您可以在 View 圖層上繪制以遮罩 Surface 的一部分,但您無法更改 Surface 圖層本身的形狀。 層是矩形的。

在許多情況下,可以使用TextureView代替 SurfaceView。 TextureView 提供了更大的靈活性,因為它由應用程序渲染到 View 層,但效率可能低於 SurfaceView。

更多信息可以在 Android 圖形架構文檔中找到

要實現您的要求,很容易做到。 您只需要使用下一個 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'

試試這個

<?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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM