简体   繁体   English

如何制作圆形表面视图

[英]How to make a rounded surfaceview

I'm trying to make a rounded shaped surfaceview.我正在尝试制作圆形的表面视图。 I've searched a lot but i couldn't find a good solution.我已经搜索了很多,但我找不到一个好的解决方案。 what i'm doing right now is that, I've put the SurfaceView into a FrameLayout, then another View on top of it, either with a PNG mask, or a shape xml drawable.我现在正在做的是,我已将 SurfaceView 放入 FrameLayout,然后在其顶部放置另一个 View,要么带有 PNG 蒙版,要么带有可绘制的形状 xml。 here it is在这里

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

But this is not a good solution and it is also not working perfectly.但这不是一个好的解决方案,它也不能完美地工作。 I want to customize surfaceview to a rounded shape.我想将表面视图自定义为圆形。 any help would be much appreciated.任何帮助将不胜感激。 Thank you :)谢谢:)

A little hack.一个小黑客。 Put your surface view inside card view.将您的表面视图放在卡片视图中。

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

Don't forget to add this to your gradle file to use CardView不要忘记将此添加到您的 gradle 文件中以使用 CardView

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

Also this two line inside card view还有这两条线在卡片视图中

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

Cheers happy coding干杯快乐编码

You can't change the shape of the SurfaceView's Surface.您无法更改 SurfaceView 的 Surface 的形状。

A SurfaceView has two parts, the Surface and the View. SurfaceView 有两个部分,Surface 和 View。 The View part works like any other View. View 部分的工作方式与任何其他 View 一样。 By default, it just acts as a transparent "hole", creating a window in the layout.默认情况下,它只是作为一个透明的“洞”,在布局中创建一个窗口。 All Views are rendered by the app onto a single layer.所有视图都由应用程序渲染到单个图层上。

The Surface part is a separate layer that sits behind the View layer (unless you explicitly change the Surface's Z order), so you only see it where it "shows through" transparent areas of the View layer. Surface 部分是位于 View 层后面的一个单独层(除非您明确更改 Surface 的 Z 顺序),因此您只能在它“通过”View 层的透明区域“显示”的地方看到它。 You can draw on the View layer to mask portions of the Surface, but you can't change the shape of the Surface layer itself.您可以在 View 图层上绘制以遮罩 Surface 的一部分,但您无法更改 Surface 图层本身的形状。 Layers are rectangular.层是矩形的。

In many situations a TextureView can be used in place of a SurfaceView.在许多情况下,可以使用TextureView代替 SurfaceView。 TextureView offers greater flexibility because it's rendered by the app onto the View layer, but can be less efficient than SurfaceView. TextureView 提供了更大的灵活性,因为它由应用程序渲染到 View 层,但效率可能低于 SurfaceView。

More information can be found in the Android graphics architecture doc .更多信息可以在 Android 图形架构文档中找到

To achieve what you ask, it is very easy to do it.要实现您的要求,很容易做到。 You only need to use the next XML:您只需要使用下一个 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>

And to use this CardView, add this dependency on your build.gradle app:要使用此 CardView,请在build.gradle应用程序中添加此依赖

implementation 'androidx.cardview:cardview:1.0.0'实现 'androidx.cardview:cardview:1.0.0'

try this试试这个

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