After setting setZOrderOnTop (true) transparency in GLSurfaceview, controls cannot be overlaid on top of GLSurfaceview and will be obscured

18 views Asked by At

The attachment is my relevant code.

Requirement: There are a total of three layers of control layout, with the lowest layer being an ImageView, the middle layer being a GLSurfaceview (with a triangle drawn), and the top layer being a Button

Now I want the lowest level Imageview not to be obscured by the GLSurfaceview, so I need to set the GLSurfaceview to transparent so that I can see the lowest level Imageview. I see that to set transparency in GLSurfaceview, setZOrderOnTop (true) must be set in order to achieve transparency. But when I set setZOrderOnTop (true), the upper control Button will be blocked by the GLSurfaceview, causing the Button text to be unclear. I have found countless methods, but no matter what, I cannot achieve transparency and unobstructed at the same time. I really don't know what to do, please help me, thank you very much!

This is the demo where the Button is obstructed

//xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />

    <RelativeLayout
        android:id="@+id/rl_GLSurfaceview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"/>


    <Button
        android:layout_centerInParent="true"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:onClick="onButtonTest"
        android:text="按钮"
        android:textColor="@color/white" />


</RelativeLayout>

//GLSurfaceView Draw Code
 glSurfaceView = new GLSurfaceView(this);
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        glSurfaceView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
        glSurfaceView.setZOrderOnTop(true);
        glSurfaceView.setRenderer(new MyGLRenderer());

        FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        RelativeLayout rlCameraARStakeout  = (RelativeLayout) findViewById(R.id.rl_GLSurfaceview);
        rlCameraARStakeout.removeAllViews();
        rlCameraARStakeout.addView(glSurfaceView, params1);


//MyGLRenderer code
package com.example.opengloverlylayer;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyGLRenderer implements GLSurfaceView.Renderer {
    private FloatBuffer vertexBuffer;
    private final int COORDS_PER_VERTEX = 3;
    private float[] triangleCoords = {
            0.0f, 0.5f, 0.0f,   // 顶点0
            -0.5f, -0.5f, 0.0f, // 顶点1
            0.5f, -0.5f, 0.0f   // 顶点2
    };
    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 每个顶点四个字节

    private int program;
    private int positionHandle;
    private int colorHandle;

    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +
                    "void main() {" +
                    "  gl_Position = vPosition;" +
                    "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
                    "uniform vec4 vColor;" +
                    "void main() {" +
                    "  gl_FragColor = vColor;" +
                    "}";

    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(triangleCoords);
        vertexBuffer.position(0);

        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        program = GLES20.glCreateProgram();
        GLES20.glAttachShader(program, vertexShader);
        GLES20.glAttachShader(program, fragmentShader);
        GLES20.glLinkProgram(program);
    }

    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 unused) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

        GLES20.glUseProgram(program);

        positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
        GLES20.glEnableVertexAttribArray(positionHandle);
        GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false,
                vertexStride, vertexBuffer);

        colorHandle = GLES20.glGetUniformLocation(program, "vColor");
        GLES20.glUniform4fv(colorHandle, 1, new float[]{1.0f, 0.0f, 0.0f, 0.7f}, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

        GLES20.glDisableVertexAttribArray(positionHandle);
    }

    private int loadShader(int type, String shaderCode) {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }
}

`

0

There are 0 answers