速读原著-Android应用开发入门教程(渲染器的实现)

10.3 渲染器的实现

在 Android 中,也可以不扩展 GLSurfaceView 类,只是实现 GLSurfaceView::Renderer 接口。实现渲染器可以在 GLSurfaceView 中直接使用。
参考示例程序:

GLSurfaceViewActivity(Graphics=>OpenGL ES=>GLSurfaceViewActivity)
TranslucentGLSurfaceViewActivity 
(Graphics=>OpenGL ES=>TranslucentGLSurfaceViewActivity)

源代码:

src/com/example/android/apis/graphics/GLSurfaceViewActivity.java 
src/com/example/android/apis/graphics/TranslucentGLSurfaceViewActivity.java 
src/com/example/android/apis/graphics/CubeRenderer.java 
src/com/example/android/apis/graphics/Cube.java

在这里插入图片描述
Cube 实现的是一个使用 OpenGL 绘制的立方体, CubeRenderer 表示基于立方体实现的渲染器,
GLSurfaceViewActivityTranslucentGLSurfaceViewActivity 分别是图中左右两个效果的界面。
CubeRenderer.java 的内容:

    class CubeRenderer implements GLSurfaceView.Renderer {
        public CubeRenderer(boolean useTranslucentBackground) {
            mTranslucentBackground = useTranslucentBackground;
            mCube = new Cube();
        }
        public void onDrawFrame(GL10 gl) {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0, 0, -3.0f);
            gl.glRotatef(mAngle, 0, 1, 0);
            gl.glRotatef(mAngle*0.25f, 1, 0, 0);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
            mCube.draw(gl);
            gl.glRotatef(mAngle*2.0f, 0, 1, 1);
            gl.glTranslatef(0.5f, 0.5f, 0.5f);
            mCube.draw(gl);
            mAngle += 1.2f;
        }
        // 省略部分内容
        private boolean mTranslucentBackground;
        private Cube mCube;
        private float mAngle;
    }

AndroidManifest.xml 中两个活动的内容:

<activity android:name=".graphics.GLSurfaceViewActivity" 
 android:label="Graphics/OpenGL ES/GLSurfaceView" 
 android:theme="@android:style/Theme.NoTitleBar" 
 android:configChanges="orientation|keyboardHidden"> 
 <intent-filter> 
 <action android:name="android.intent.action.MAIN" /> 
 <category android:name="android.intent.category.SAMPLE_CODE" /> 
 </intent-filter> 
 </activity> 
 <activity android:name=".graphics.TranslucentGLSurfaceViewActivity" 
 android:label="Graphics/OpenGL ES/Translucent GLSurfaceView" 
 android:theme="@style/Theme.Translucent" 
 android:configChanges="orientation|keyboardHidden"> 
 <intent-filter> 
 <action android:name="android.intent.action.MAIN" /> 
 <category android:name="android.intent.category.SAMPLE_CODE" /> 
 </intent-filter> 
 </activity>

2 个程序主要的区别是 TranslucentGLSurfaceViewActivity 通 过 android:theme 将背景颜色设置成了
Theme.Translucent,表示透明的背景。

GLSurfaceViewActivity.java 的内容如下所示:

    public class GLSurfaceViewActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGLSurfaceView = new GLSurfaceView(this);
            mGLSurfaceView.setRenderer(new CubeRenderer(false));
            setContentView(mGLSurfaceView);
        }
    }

TranslucentGLSurfaceViewActivity.java 的内容:

    public class TranslucentGLSurfaceViewActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGLSurfaceView = new GLSurfaceView(this);
            // 使用 RGB8888 的像素格式 
            mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
            // 定义下层为透明
            mGLSurfaceView.setRenderer(new CubeRenderer(true));
            // 设置 Surface 的 Alpha 通道
            mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            // 设置 Surface 透明
            setContentView(mGLSurfaceView);
        }
    }

以上的 2 个程序中,重了后者设置了透明效果外基本相同,在这 2 个实现中,不需要重新实现 GLSurfaceView,而只是实现 GLSurfaceView 中::Renderer 即可,

发布了1049 篇原创文章 · 获赞 868 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/103972397
今日推荐