代码之家  ›  专栏  ›  技术社区  ›  visionix visionix

如何创建圆角曲面视图

  •  0
  • visionix visionix  · 技术社区  · 10 年前

    我很难绕过surfaceview的角落。我正在使用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);
                    }
                }
            }
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   18446744073709551615    10 年前

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

    1. 创建 <FrameLayout ...> 有两个孩子:表面视图和上面的部分透明覆盖层( FrameLayout 将其子对象一个接一个地绘制。)

    2. 在覆盖层上绘制圆角。

    也就是说,您将在 SurfaceView 并且不透明层将在其中心具有带圆角的透明孔。

    这里有一个例子,我用50%透明的黑色层覆盖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"
            >
            ...
    
        2
  •  0
  •   Nessie    5 年前

    要实现您的要求,很容易做到。您只需要使用下一个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,请在 建筑物玻璃 应用程序:

    implementation 'androidx.cardview:cardview:1.0.0'