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

文章源地址:
http://www.waitingfy.com/?p=28

你可能感兴趣的:(Android,2D,游戏开发,Android,中使用OpenGL,ES进行2D开发,android,float,interface,class,buffer)