Android GLSurfaceView transparent background without setZOrderonTop
Asked Answered
E

6

18

Sorry for my English.

My work is based on https://github.com/harism/android_page_curl/

After many hours of research, I have found a few solutions, but not for all the issues what I have in my application. I've some trouble with a GLSurfaceView. I've got a background with a relativeLayout, a GLSurfaceView, and an overlay on top.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:background="@layout/backgroundrepeat"
    android:layout_height="match_parent">
<com.m2y.foxprez.view.CurlView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/openglview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</RelativeLayout>

When I init my view :

setEGLConfigChooser(8,8,8,8,16,0);
setRenderer(renderer);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
setZOrderMediaOverlay(true);

In my renderer:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0f, 0f, 0f, 0f);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);
    gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
    gl.glEnable(GL10.GL_LINE_SMOOTH);
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glDisable(GL10.GL_CULL_FACE);
}

public synchronized void onDrawFrame(GL10 gl) {
    gl.glClearColor(0f,0f,0f,0f);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();
}

After multiple research, I am currently blocked with this result:
with setZOrderOnTop, i've got background, but the overlay is under the view.
with setZOrderMediaOverlay, i've got the overlay, but the background is black.

Someone can help me?

Endometrium answered 26/5, 2013 at 18:56 Comment(0)
V
16

This worked for me:

final GLSurfaceView glView = (GLSurfaceView) findViewById(R.id.glView);

glView.setZOrderOnTop(true);
glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glView.getHolder().setFormat(PixelFormat.RGBA_8888);
glView.setRenderer(new MyRenderer());
glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

Found here: http://andrewhague.wordpress.com/2011/05/24/how-to-set-a-transparent-glsurfaceview/

Volin answered 1/6, 2013 at 13:2 Comment(4)
Can you put some overlay front on your opengl view? I've found some other solution, like do my overlay in opengl, but if someone have a solution that can help me edit : when i speak about overlay, i speak about a relative layout (not an element of my opengl view)Endometrium
I didn't try. Actually, I found this while trying the opposite.Doglike
I tried this thing in my code but its not working, are there any problem in setting up shaders?Disaccustom
It says in a question "without setZOrderonTop", why almost all answers contain this method?Transmit
H
3

I would like to accomplish the same thing with a view on top of a GLSurfaceView and I have not found a solution without limitations. First, glView.setZOrderOnTop(true) gives the correct transparent background but the view is alway on top no matter what the sequence of ordering views. Second, without ZOrderOnTop the view hierarchy is maintained but the GLSurface background is black or the color that is set with glClearColor(red, green, blue, alpha) in the renderer. Unfortunately, the alpha argument has no effect when using this method unless

android:windowIsTranslucent="true"

and the problem with this is that you may not want to punch a hole into the OS view. There should be a way to accomplish this easily for a more animated UI using openGL. I have searched this site and others without finding a solution.

Heaton answered 6/2, 2014 at 20:19 Comment(0)
B
0

This question already has an answer for the general case. If you're looking for a solution with LibGDX, here it is.

@DarioCastañé 's answer but with LibGDX 1.9.12:

(Kotlin code):

val config = AndroidApplicationConfiguration().apply {
    r = 8
    g = 8
    b = 8
    a = 8
    depth = 16
    stencil = 0
}

val view = initializeForView(MyGame(), config)

if (view is GLSurfaceView) {
    view.setZOrderOnTop(true)
    view.holder.setFormat(PixelFormat.RGBA_8888)
}

return view // in Fragment
setContentView(view) // in Activity

in your render() method:

Gdx.gl.glClearColor(0f, 0f, 0f, 0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

This, under the hood, will do exactly the same thing.

Billiot answered 25/1, 2021 at 16:15 Comment(0)
A
0

I had a similar problem, different solution. I was able to order the two openGL views from onCreate like this (Kotlin):

    viewBinding.view1.setZOrderOnTop(true) // On top of everything.
    viewBinding.view2.setZOrderOnTop(true) // On top of view1

or similarly using findViewByID() in Java.

Actinal answered 6/6, 2022 at 21:27 Comment(0)
P
-1

I'm working the same problem. I found that you have to make the calls for

setZOrderOnTop(true);
getHolder().setFormat(PixelFormat.RGBA_8888);

before the call to

setRenderer(...);

Good luck!

Papyraceous answered 9/3, 2018 at 18:10 Comment(0)
H
-1

I was also facing the same issue that nothing view was showing when I was using GLSurfaceView.

After getting the reference of GLSurfaceView do like this

glSurface=findViewById(R.id.gl_surfaceview)

//this is the import line only

glSurface.setZOrderOnTop(false)

now my all view are visible on top of GLSurfaceView

Thanks.

Hexachlorophene answered 31/7, 2018 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.