[英]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.