- Testing equipment supported versions, to determine whether to support opengl version 2.0
- Initial Setup OpenGLES2.0
- Implement the interface rendering GLSurfaceView.Renderer
- Graphing
1, the detection device support version, version 2.0 is determined whether to support opengl
private boolean hasGLES20(){ ActivityManager activityManager=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); ConfigurationInfo info=activityManager.getDeviceConfigurationInfo(); return info.reqGLEsVersion>=0x20000; }
2, mandatory application support
<uses-feature android:glEsVersion="0x00020000"
android:required="true"/>
3, initial setup OpenGLES2.0
@Override protected void onCreate(Bundle savedInstanceState){ //设置全屏模式 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); if(hasGLES20()){ GLSurfaceView mGLView=new GLSurfaceView(this); mGLView.setEGLContextClientVersion(2); //保留OpenGLESs上下文 mGLView.setPreserveEGLContextOnPause(true); mGLView.setRenderer(new GLES20Renderer()); } Super .onCreate (savedInstanceState); the setContentView (mGLView); } // to work properly, to keep the life cycle corresponding to the OpenGL Activity @Override protected void onResume () { Super .onResume (); IF (! mGLView = null ) { mGLView.onResume (); } } @Override protected void the onPause () { Super .onPause (); IF (mGLView =! null ) { mGLView.onPause (); } }
4, GLSurfaceView.Renderer renderer, rendering implement the interface GLSurfaceView.Renderer
public abstract class GLRenderer implements GLSurfaceView.Renderer{ private boolean mFirstDraw; private boolean mSurfaceCreated; private int mWidth; private int mHeight; private long mLastTime; private int mFPS; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { mSurfaceCreated=true; mWidth=-1; mHeight=-1; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { if (!mSurfaceCreated&&width==mWidth&&height==mHeight){ return; } mWidth=width; mHeight=height; onCreate(mWidth,mHeight,mSurfaceCreated); mSurfaceCreated=false; } @Override public void onDrawFrame(GL10 gl) { onDrawFrame(mFirstDraw); if (mFirstDraw){ mFirstDraw=false; } } public abstract void onCreate(int width,int height,boolean contextLost); public abstract void onDrawFrame(boolean firstDraw); public int getFPS(){ return mFPS; } }
5, draw graphics
public class GLES20Renderer extends GLRenderer { @Override public void onCreate(int width, int height, boolean contextLost) { GLES20.glClearColor(0f, 0f, 0f, 1f); } @Override public void onDrawFrame(boolean firstDraw) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); } }