Android 中使用OpenGL ES进行2D开发(GLSurfaceView)
16lz
2021-01-23
我们知道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
更多相关文章
- Android程序实现全屏代码
- [原]Android有用代码片断(六)
- android shape 代码实现按钮背景
- 获取Android SDK 源代码并在Eclipse中关联查看的方法(for sdk2.3
- Android开发实例源代码批量下载
- Android实现游戏摇杆的源代码
- Android Alarm驱动源代码分析(Alarm.c)