OpenGL ES实现背景透明

2018-03-27  本文已影响0人  Ricky_Zeng

随便实现了这么一个盒子,想给它添加一个看起来舒服点的背景。效果图如下:


效果图.png

问题:需要通过布局文件直接设置background(android:background="#e6e6e6"),会由于GLSurfaceView的背景而无法直接通过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"
    android:orientation="vertical"
    android:background="#e6e6e6">

    <com.gl3d.render.GL3DSurfaceView
        android:id="@+id/gl3d_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

解决:将GLSurfaceView的背景设置为透明后,即可直接在展示的Activity的XML布局文件中实现background属性的直接设置。

实现步骤:
1.在调用GLSurfaceView.setRenderer()之前调用:

            GLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
            GLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            GLSurfaceView.setZOrderOnTop(true);

2.在GLSurfaceView.Renderer的onSurfaceCreated方法里,把背景的透明度设为0:

GLES20.glClearColor(0,0,0,0);
上一篇下一篇

猜你喜欢

热点阅读