SurfaceView 如何设置圆角?

5,407 阅读1分钟

1)android 5.0之后,使用ViewOutlineProvider,适用于任何view。

原理就是通过设置mRenderNode 直接GPU 硬件加速,不走Draw 流程;SurfaceView 区域大小就是设置了圆角的View。

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun setRoundRectShape(radius: Float) {
        this.clipToOutline = true
        this.outlineProvider = ViewRoundRectOutlineProvider(radius)
    }

以上方法适用于Activity 中嵌入SurfaceView,且SurfaceView的parent 不为透明。

parent 为非透明

2)如果以悬浮窗的方式添加SurfaceView则不然,Parent 为透明。

因为悬浮窗的展示形式,parent为透明的系统View,所以即使我们使用ViewOutlineProvider 截取了圆角,因为SurfaceView 会设置一个Region 区域把parent 设置为透明,所以我们看到的还是有边角的surfaceView。 此问题暂时无解。