Android 中使用OpenGL ES进行2D开发(GLSurfaceView)

我们知道OpenGL一般是在C,C++中应用,那么Android如何跟OpenGL ES对接的呢?

是用GLSurfaceView,今天我们的主角

interface GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config);
public void onSurfaceChanged(GL10 gl, int width, int height);
public void onDrawFrame(GL10 gl);
}

在Android中有上面代码中涉及到的接口(interface)

现在来看下我们一个简单的示例的效果,整个红色的屏幕

我们来看下完整的代码 GLSurfaceViewTest.java, 路径src/com.waitingfy.android.glbasics/GLSurfaceViewTest.java

    package com.waitingfy.android.glbasics;

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

    import android.app.Activity;
    import android.opengl.GLSurfaceView;
    import android.opengl.GLSurfaceView.Renderer;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Window;
    import android.view.WindowManager;

    public class GLSurfaceViewTest extends Activity {
        GLSurfaceView glView;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //去掉activity的标题,全屏显示
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            glView = new GLSurfaceView(this);
            glView.setRenderer(new SimpleRenderer());
            setContentView(glView);
        }

        @Override
        public void onResume() {
            super.onPause();
            glView.onResume();
        }

        @Override
        public void onPause() {
            super.onPause();
            glView.onPause();
        }

        static class SimpleRenderer implements Renderer {

            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                Log.d("GLSurfaceViewTest", "surface created");
            }
            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
                Log.d("GLSurfaceViewTest", "surface changed: " + width + "x"
                        + height);
            }
            @Override
            public void onDrawFrame(GL10 gl) {
                //设置颜色为红色(glClearColor(float red, float green, float blue, float alpha)
                gl.glClearColor(1, 0, 0, 1);
                gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            }
        }
    }

非常清晰的代码,好了有关GLSurfaceView的内容就到这里 下面是示例代码下载:gl-basics

28

5 Responses to Android 中使用OpenGL ES进行2D开发(GLSurfaceView)

  1. web design说道:

    Thank you for any other informative website. Where else could I get that kind of info written in such an ideal way? I’ve a project that I am simply now running on, and I have been on the look out for such information.

  2. yinX说道:

    我运行Texture那个Test,崩溃了诶

  3. herbert说道:

    mark~

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.