我们知道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 透明效果
  2. Android(安卓)Edittext设置负数以及小数
  3. 几款好用的Android(安卓)Studio插件
  4. TextView添加ClickableSpan和LinkMovementMethod之间的关系
  5. android中的提示信息显示方法(toast应用)
  6. Android实现widget定时更新
  7. Android中实现下载和解压zip文件功能代码分享
  8. android 播放音乐示例
  9. 在Android(安卓)Service中弹出系统全屏对话框

随机推荐

  1. 编程回忆之Android回忆(蓝牙BluetoothAdap
  2. mono for android 第三课--页面布局
  3. android volley解析与二次封装
  4. Android获取root权限的利弊关系
  5. android Activity布局初步(三)- 相对布局
  6. Ohone SDK的模拟器居然不能联网??
  7. Android中常见的流式布局的使用
  8. android 5.0 创建多用户 双开多开应用(1)
  9. Android(java)学习笔记128:使用proguard混
  10. 华尔街日报:Android(安卓)TV应用将在Goog