【问题标题】:How to create surfaceview with rounded corners如何创建带有圆角的表面视图
【发布时间】:2015-03-12 10:37:06
【问题描述】:

我很难绕过surfaceview的角落。我正在使用 MjpegView (继承自surfaceview的自定义视图。 我已经尝试过这个解决方案: 1)用圆角自定义drawable设置背景 2) 看完这篇文章http://androidtutorialsandtips.blogspot.co.il/2012/01/overriding-ondraw-method-in-surfaceview.html 我不确定如何实现圆角,因为我已经实现了一个锁定画布的线程。

while (mRun)
        {
            if (surfaceDone)
            {
                try
                {
                    c = mSurfaceHolder.lockCanvas();
                    synchronized (mSurfaceHolder)
                    {
                        try
                        {
                            if (mIn != null)
                            {
                                Bitmap bm = mIn.readMjpegFrame();
                                destRect = destRect(bm.getWidth(), bm.getHeight());

                                if (streamHeight == -1 && streamWidth == -1)
                                {
                                    streamWidth = bm.getWidth();
                                    streamHeight = bm.getHeight();
                                }
                                c.drawColor(Color.BLACK);
                                c.drawBitmap(bm, null, destRect, p);
                                if (showFps)
                                {
                                    p.setXfermode(mode);
                                    if (ovl != null)
                                    {
                                        height = ((ovlPos & 1) == 1) ? destRect.top : destRect.bottom - ovl.getHeight();
                                        width = ((ovlPos & 8) == 8) ? destRect.left : destRect.right - ovl.getWidth();
                                        c.drawBitmap(ovl, width, height, null);
                                    }
                                    p.setXfermode(null);
                                    frameCounter++;
                                    if ((System.currentTimeMillis() - start) >= 1000)
                                    {
                                        fps = String.valueOf(frameCounter) + "fps";
                                        frameCounter = 0;
                                        start = System.currentTimeMillis();
                                        ovl = makeFpsOverlay(overlayPaint, fps);
                                    }
                                }
                            }
                        }
                        catch (IOException e)
                        {
                        }
                    }
                }
                finally
                {
                    if (c != null) mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }

【问题讨论】:

  • 你有什么问题?
  • 我无法绕过surfaceview的角落
  • 什么是模式?为什么你设置它但从不使用它?你的代码的结果是什么?
  • 我正在使用这个视图code.google.com/p/android-camera-axis/source/browse/trunk/…。结果很简单,表面视图有角,我需要将它们移除。我尝试更改表面视图的背景,但它不起作用
  • FWIW,任何建议覆盖 SurfaceView 的 onDraw() 的方法都可能是错误的。 SurfaceView 表面不是 View 层次结构的一部分,不应与之绑定。请记住,surface 位于一个单独的层上,位于所有 View UI 之后,并且与无效/刷新周期无关,事情会更有意义。如下面的回答中所述,将不透明的圆角图像放在叠加层上,是做你想做的最简单的方法。

标签: android stream surfaceview


【解决方案1】:

如果我正确理解您的问题:

  1. 创建一个有 2 个孩子的 <FrameLayout ...>:表面视图和上面的部分透明覆盖层。 (FrameLayout 将其子代绘制在另一个之上。)

  2. 在覆盖层上画圆角。

也就是说,您将在SurfaceView 上方有一个不透明层,并且该不透明层的中心将有一个带圆角的透明孔。

这是一个示例,其中我覆盖了一个具有 50% 透明黑色层(默认为 visibility="invisible")的 SurfaceView 和一个内部带有按钮的 RelativeLayout。

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SurfaceView
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true" />
    <View
        android:id="@+id/transparency"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#80000000"
        android:visibility="invisible"
        />
    <RelativeLayout
        android:id="@+id/xxxxx"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        ...

【讨论】:

  • 你能给我举个例子吗,如果你只是在封面布局中添加圆角的drawable,你仍然会看到surfaceview的角
  • 我的意思是,surfaceView 将被带有圆形不透明角的透明图像覆盖。只需尝试将任何图像放在表面视图上方,然后您就会看到该怎么做...
  • @visionixvisionix:你能实现这个吗?如果是,请您发布一些示例代码。就我而言,我想以圆形显示相机
【解决方案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 应用中添加此依赖项:

implementation 'androidx.cardview:cardview:1.0.0'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2019-11-14
    • 1970-01-01
    • 2020-06-26
    • 2010-12-18
    相关资源
    最近更新 更多