【问题标题】:How to make a rounded surfaceview如何制作圆形的表面视图
【发布时间】:2016-08-02 12:28:55
【问题描述】:

我正在尝试制作一个圆形的表面视图。我已经搜索了很多,但我找不到一个好的解决方案。我现在正在做的是,我已将 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>

但这不是一个好的解决方案,它也不能完美地工作。我想将surfaceview自定义为圆形。任何帮助将非常感激。谢谢你:)

【问题讨论】:

  • stackoverflow.com/questions/26074784/… ...试试这个..将FrameLayout更改为SurfaceView
  • 感谢您的参与。 @sunilsunny,您可以使用它而不是surfaceview 使任何视图四舍五入。我已经试过了:)
  • @TashenJazbi:你找到解决方案了吗?我遇到了同样的实施问题。你能发布你的解决方案吗?谢谢
  • @Sneha 我们无法改变 SurfaceView 的 Surface 形状。请仔细阅读正确答案:)
  • @TashenJazbi:是的没错,那你是怎么实现的呢?

标签: android surfaceview


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      一个小技巧。将您的表面视图放在卡片视图中。

      <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 在另一个 SurfaceView 之上时不起作用
      • 这确实有效。实际上,它与覆盖 dispatchDraw 和剪切画布相同。但我必须同意@ralphgabb,这两个技巧(dispatchDraw 和 cardView)都不适用于 SurfaceView 在另一个 SurfaceView 之上。
      • 实际上不是 :) 因为在我的情况下,我需要在另一个 SurfaceView 上方绘制 SurfaceView。在所有其他情况下,您的解决方案应该可以正常工作。
      • 您也可以使用带有圆角背景的布局,例如FrameLayout。然后将 SurfaceView 添加为框架布局的子级,并在 FrameLayout 上将 setClipToOutline 设置为 true。但它不能在另一个 SurfaceView 之上工作
      • 如果您使用的是 Camerax PreviewView,请将 PreviewView 的 implementationMode 设置为 compatible,这会强制它使用 TextureView 而不是 SurfaceView,您将获得圆角。
      【解决方案4】:

      你不能改变 SurfaceView 的 Surface 的形状。

      SurfaceView 有两部分,Surface 和 View。 View 部分的工作方式与任何其他 View 一样。默认情况下,它只是充当一个透明的“洞”,在布局中创建一个窗口。所有视图都由应用程序渲染到单个图层上。

      Surface 部分是位于 View 层后面的单独层(除非您明确更改 Surface 的 Z 顺序),因此您只能在它“透过”View 层的透明区域“显示”的地方看到它。您可以在 View 图层上绘制以遮盖 Surface 的某些部分,但不能更改 Surface 图层本身的形状。层是矩形的。

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

      更多信息可以在Androidgraphics architecture doc找到。

      【讨论】:

      • 您的答案听起来很合理,可以接受它作为正确答案。感谢您的参与:)
      • 我的用例是使用CameraX PreviewView 在另一个表面视图的顶部显示相机预览。 PreviewView 使用它在运行时决定的 TextureViewSurfaceView。当设置为显式使用TextureView(通过将implementationMode 属性设置为compatible)时,我可以获得圆角。我将我的 PreviewView 放在了 @ralphgabb 建议的 CardView 中。
      猜你喜欢
      • 2017-01-19
      • 2014-07-01
      • 1970-01-01
      • 2021-02-05
      • 2021-01-12
      • 2013-05-04
      • 1970-01-01
      • 2016-09-17
      • 2017-11-20
      相关资源
      最近更新 更多