我们知道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);}@Overridepublic void onResume() {super.onPause();glView.onResume();}@Overridepublic void onPause() {super.onPause();glView.onPause();}static class SimpleRenderer implements Renderer {@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {Log.d("GLSurfaceViewTest", "surface created");}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {Log.d("GLSurfaceViewTest", "surface changed: " + width + "x"+ height);}@Overridepublic 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

更多相关文章

  1. Android(安卓)Contacts(一)—— 读取联系人
  2. Android(安卓)Contacts(一)—— 读取联系人
  3. Android学习感想一
  4. [原]Android有用代码片断(六)
  5. android shape 代码实现按钮背景
  6. android shape 代码实现按钮背景
  7. [笔记分享] [Android] Android系统概述
  8. android与javascript交互调用
  9. Android(安卓)使用Vitamio打造自己的万能播放器(4)――本地播放(快

随机推荐

  1. 2011.06.23——— android 事件处理机制
  2. Android实时绘制效果(一)
  3. android eclipse setup
  4. 使用命令行编译Qt Android apps
  5. android 的relativelayout 报错 No resou
  6. Android 用AsyncTask下载网络图片并显示
  7. Android SDK 自带项目GestureBuilderActi
  8. Android 判断当前网络连接是否可用、打开
  9. android 环境配置
  10. Android的API版本和名称对应关系