GLSurfaceView - how to make translucent background

后端 未结 5 1442
日久生厌
日久生厌 2021-01-04 19:08

I try to render using GLSurfaceView, and by docs I set format:

getHolder().setFormat(PixelFormat.TRANSLUCENT);

The I use GLSurfaceView.Rend

相关标签:
5条回答
  • 2021-01-04 19:47

    I'm not sure what the problem is exactly but an alternative could be covering the screen with a translucent colored rectangle and just clearing the depth buffer bit.

    Is there any alternative for GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)?

    Otherwise make sure your depth buffer bit is 1

    I'll be back later to try and recreate the problem on my phone in a bit when I can.

    Edit: I just realized that the reason it appears blue may be that you set it to .5f so it only takes 2 calls for it to get to full 1f opacity. meaning that it takes at 30fps 1/15th of a second to go fully blue.

    Try lowering the alpha transparency value to 0.01f or 0.05f

    0 讨论(0)
  • 2021-01-04 19:50

    Have you thought about using a LayerDrawable with setAlpha? Here's an example i have of two images... one is transparent (by the setAlpha) and the other is not. The 'calendar_cell' is solid and is on the back, then comes the box which is transparent to show the calendar cell behind it. This way you can stack as many images as you want giving them all a different transparency.

    Drawable []layers = new Drawable [2];
    int imageResource1 = mContext.getResources().getIdentifier("drawable/calendar_cell", null, mContext.getPackageName());
    Drawable background = v.getResources().getDrawable(imageResource1);
                            layers [0]= background;
    
    int imageResource = mContext.getResources().getIdentifier("drawable/box_" + box, null, mContext.getPackageName());
    Drawable boxImg = v.getResources().getDrawable(imageResource);
        boxImg.setAlpha(100);
        layers [1]= boxImg;
    
    LayerDrawable layerDrawable = new LayerDrawable (layers);
    v.setBackground(layerDrawable)
    
    0 讨论(0)
  • 2021-01-04 19:59

    try using setZOrderMediaOverlay(true) for the GLSurfaceView with Hardware Acceleration for the Activity set to false. That makes the GLSurfaceView background Transparent. But I am not sure how the hardware acceleration impacts it.
    Overlaying a GLSurface on top of a MapView without using SetZOrderTop.
    I had posted a question recently still looking for a better solution

    0 讨论(0)
  • 2021-01-04 20:02

    You need RGBA 8888 pixel format for translucency:

    private void init( boolean translucent, int depth, int stencil )
    {
        /* By default, GLSurfaceView() creates a RGB_565 opaque surface.
         * If we want a translucent one, we should change the surface's
         * format here, using PixelFormat.TRANSLUCENT for GL Surfaces
         * is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
         */
        this.getHolder().setFormat( PixelFormat.RGB_565 );
        if ( translucent )
        {
            this.getHolder().setFormat( PixelFormat.TRANSLUCENT );
        }
    
        setEGLContextFactory( new ContextFactory() );
    
        /* We need to choose an EGLConfig that matches the format of
         * our surface exactly. This is going to be done in our
         * custom config chooser. See ConfigChooser class definition
         * below.
         */
        setEGLConfigChooser( translucent ?
                             new ConfigChooser( 8, 8, 8, 8, depth, stencil ) :
                             new ConfigChooser( 5, 6, 5, 0, depth, stencil ) );
    
        setRenderer( new Renderer() );
    }
    
    0 讨论(0)
  • 2021-01-04 20:04

    OK, after some research I can answer this myself.

    I finally made it to be drawn with transparency using SurfaceView.setZOrderOnTop(true). But then I lost possibility to place other views on top of my GLSurfaceView.

    I ended with two possible results:

    behind on top

    On left is standard GL surface below all other views, which can't be drawn with transparency, because GL surface is drawn before application's window surface, and GLSurfaceView just punches hole in its location so that GL surface is seen through.

    On right is transparent GL surface drawn with setZOrderOnTop(true), thus its surface is drawn on top of application window. Now it's transparent, but is drawn on top of other views placed on it in view hierarchy.

    So it seems that application has one window with surface for its view hierarchy, and SurfaceView has own surface for GL, which may be on top or below of app window. Unfortunately, transparent GL view can't be ordered correctly inside view hierarchy with other views on top of it.

    0 讨论(0)
提交回复
热议问题