在application中用openGL ES 来花图的话,必须要创建一个GLSurfaceView 和 GLSurfaceView.Renderer作为容器。
要使用OpenGL es 2.0 API的话,首先要在manifest文件中声明权限.
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
如果使用texture compression的话,也要申请权限.
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
可以像使用TextView一样来使用GLSurfaceView
public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
}
}
这个Activity中有new 一个GLSurfaceView 对象,我们看看是如何定义的.
class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer mRenderer;
public MyGLSurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
mRenderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(mRenderer);
}
}
我们再来看看MyGLRenderer 的实现.
public class MyGLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
onSurfaceCreated() - 调用一次建立view 的OpenGL ES环境
onDrawFrame() - Called for each redraw of the view.
onDrawFrame() -每次重新绘图的时候都调用.
onSurfaceChanged() - 当view的几何图形变化时。例如横屏时.
要使用OpenGL es 2.0 API的话,首先要在manifest文件中声明权限.
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
如果使用texture compression的话,也要申请权限.
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
可以像使用TextView一样来使用GLSurfaceView
public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
}
}
这个Activity中有new 一个GLSurfaceView 对象,我们看看是如何定义的.
class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer mRenderer;
public MyGLSurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
mRenderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(mRenderer);
}
}
我们再来看看MyGLRenderer 的实现.
public class MyGLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
onSurfaceCreated() - 调用一次建立view 的OpenGL ES环境
onDrawFrame() - Called for each redraw of the view.
onDrawFrame() -每次重新绘图的时候都调用.
onSurfaceChanged() - 当view的几何图形变化时。例如横屏时.