简体   繁体   English

如何创建带圆角的表面视图

[英]How to create surfaceview with rounded corners

I am having a difficulty to round the corners of surfaceview.我很难绕过surfaceview的角落。 I am using MjpegView (custom view that inherit from surfaceview. I tried already this solutions: 1) set background with custom drawable with round corners 2) After reading this post http://androidtutorialsandtips.blogspot.co.il/2012/01/overriding-ondraw-method-in-surfaceview.html I am not sure how to implement the round corners as I have already implemented a thread that locks the canvas.我正在使用 MjpegView(从surfaceview 继承的自定义视图。我已经尝试过这个解决方案:1)使用带有圆角的自定义可绘制设置背景 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);
                }
            }
        }

If I understand your question correcly:如果我正确理解您的问题:

  1. create a <FrameLayout ...> with 2 children: the surface view and a partially transparent cover layer above it.创建一个带有 2 个孩子的<FrameLayout ...> :表面视图和它上面的部分透明的覆盖层。 ( FrameLayout draws its children one on top of another.) FrameLayout将其子项绘制在另一个之上。)

  2. draw the rounded corners on the cover layer.在覆盖层上绘制圆角。

That is, you will have an opaque layer above the SurfaceView and that opaque layer will have a transparent hole-with-rounded-corners in its center.也就是说,您将在SurfaceView上方有一个不透明层,该不透明层将在其中心有一个带圆角的透明孔。

Here's an example where I cover a SurfaceView with a 50%-transparent black layer ( visibility="invisible" by default) and a RelativeLayout with buttons inside.这是一个示例,我用 50% 透明的黑色图层(默认情况下可见性 =“invisible” )和一个带有按钮的 RelativeLayout 覆盖 SurfaceView。

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

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM