首页 > 解决方案 > GLSurfaceView 在其他视图之上

问题描述

我想GLSurfaceView在我的活动上显示当前内容作为覆盖。但是下面的内容需要可见。

这是我的设置GLSurfaceView

setEGLConfigChooser(8, 8, 8, 8, 16, 0)
setZOrderOnTop(true)
holder.setFormat(PixelFormat.TRANSLUCENT)
setRenderer(renderer)

在渲染器中我有:

    override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
        // Set the background frame color
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.1f)
    }

    override fun onDrawFrame(unused: GL10) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
    }

    override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

我可以看到它是半透明的,但透明度绝对不是 alpha 值的 10%。它要高得多。即使将清除颜色设置为 (1, 0, 0, 0),我也可以在主要内容之上看到非常饱和的红色叠加层。

任何想法如何在我的主要内容之上呈现 GLSurfaceView 并支持透明度?

标签: androidopengl-esglsurfaceview

解决方案


作为变体。您可以在单独的视图元素中显示 OpenGLES 渲染,并为其余活动内容设置透明度,例如:

<com.app.CustomSurfaceView
    android:id="@+id/oglView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.5"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/button"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textSize="38sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果:

在此处输入图像描述


推荐阅读