Android GLSurfaceView设置透明背景及放置在顶层显示

GLSurfaceView本身是不透明的,在相机预览图像上添加水印有时会采用在surface view上附加一层GLSufaceView绘制各种各样的水印图片,这时需要透明背景的GLSurfaceView并放置在上层显示,可以通过如下方式实现:

class MyGLSurfaceView(context: Context, attributes: AttributeSet?) : GLSurfaceView(context, attributes) {

    constructor(context: Context) : this(context, null)

    private val renderer: MyGLRenderer

    init {

        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2)

        setTranslucent()

        renderer = MyGLRenderer()

        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(renderer)

        // Render the view only when there is a change in the drawing data
        renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
    }

    /**
     * 设置透明背景的方法
     * 这个方法需要在setRenderer之前调用才有效
     */
    private fun setTranslucent(){
        setEGLConfigChooser(8, 8, 8, 8, 16, 0)
        holder.setFormat(PixelFormat.TRANSLUCENT)
        // 将GLSurfaceView置顶
        setZOrderOnTop(true)
    }
}

你可能感兴趣的:(Android,OpenGL)