首页 > 代码库 > Android OpenGlL GLSurfaceView基本实现方法
Android OpenGlL GLSurfaceView基本实现方法
GLSurfaceView 是一个视图,继承至SurfaceView,它内嵌的surface专门负责OpenGL渲染。
在使用的时候要实现以下几步。
一、创建自定义1类继承自GLSurfaceView,并创建构造器
二、创建自定义2类实现GLSurfaceView.Renderer接口
重写onDrawFrame(GL10 gl)、 onSurfaceChanged(GL10 gl, int width, int height)、onSurfaceCreated(GL10 gl, EGLConfig config)方法
三、在自定义1中定义自定义类2,并在自定义类1的构造方法中调用setRenderer(renderer);方法进行渲染器设置
四、在主Ativity中创建自定义类1,并将其设置为视图,为了使GLSurfaceView能够与Activity同步,要重写Activity的onPouse和onResume方法,并分别在相应的方法中调用GLSurfaceView的onPouse和onResume方法。
*********以下一简单的GLSurfaceView应用,点击屏幕,根据屏幕坐标设置屏幕颜色。*******
package com.example.android_opengl;
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL; import javax.microedition.khronos.opengles.GL10;
import android.content.Context; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.util.Log; import android.view.MotionEvent;
public class MyGLSurfaceview extends GLSurfaceView{ MyGLRender myRender; float vertexArray[]=new float[]{ -0.8f,-0.4f*1.732f,0.0f, 0.8f,-0.4f*1.732f,0.0f, 0.0f,0.4f*1.732f,0.0f }; ByteBuffer vbb=ByteBuffer.allocateDirect(vertexArray.length*4); FloatBuffer fb; public MyGLSurfaceview(Context context) { super(context); // TODO Auto-generated constructor stub vbb.order(ByteOrder.nativeOrder()); fb=vbb.asFloatBuffer(); fb.put(vertexArray); fb.position(0); myRender=new MyGLRender();
setRenderer(myRender); }
@Override public boolean onTouchEvent(final MotionEvent event) { // TODO Auto-generated method stub queueEvent(new Runnable() { @Override public void run() { // TODO Auto-generated method stub myRender.setColor(event.getX() / getWidth(), event.getY() / getHeight(),1); } }); return super.onTouchEvent(event); } private class MyGLRender implements Renderer{
@Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub gl.glClearColor(mRed, mGreen,mBlue, 0.8f);//设置屏幕颜色 gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);// 清空缓存
}
@Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub gl.glViewport(0, 0, width, height);//设置窗口 }
@Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub } public void setColor(float r, float g, float b) { mRed = r%10; mGreen = g%10; mBlue = b%10; System.out.println("R "+mRed+" G"+mGreen+" B"+mBlue); } private float mRed; private float mGreen; private float mBlue; } }
package com.example.opengl_huizhisanjiaoxing;
import android.os.Bundle; import android.provider.Settings.Global; import android.app.Activity; import android.view.Menu;
public class MainActivity extends Activity {
MyGLSurfaceView GLview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLview=new MyGLSurfaceView(this); setContentView(GLview); }
@Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); GLview.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); GLview.onResume(); }
}
Android OpenGlL GLSurfaceView基本实现方法