GLSurfaceView configuration
First determine the version of opengles used, then set the specified renderer, and finally display it on Activity.
Note that in Activity's life cycle function, you control the start and pause of GLSurfaceView rendering.
public class MainActivity extends AppCompatActivity { private static final String TAG = "opengl-demos"; private static final int GL_VERSION = 3; private GLSurfaceView glSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Initialize GLSurfaceView glSurfaceView = new GLSurfaceView(this); //Check whether OpenGLES 3.0 is supported if (!checkGLVersion()){ Log.e(TAG, "not supported opengl es 3.0+"); finish(); } //Using opengles 3.0 glSurfaceView.setEGLContextClientVersion(GL_VERSION); //Setting up the renderer glSurfaceView.setRenderer(new DemoRender()); //Display GLSurfaceView to Activity setContentView(glSurfaceView); } private boolean checkGLVersion(){ ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); ConfigurationInfo ci = am.getDeviceConfigurationInfo(); return ci.reqGlEsVersion >= 0x30000; } @Override protected void onResume() { super.onResume(); //Execution rendering glSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); //Pause rendering glSurfaceView.onPause(); } }
Implementing Render
Implementing GLSurfaceView.Renderer interface can customize the renderer.
Three methods are defined in the interface:
- OnSurface Created: When Surface is created, the method is called by GLSurfaceView. Programs are called when they are created, and app s can be called when they are switched.
- OnSurface Changed: Called when the size of Surface changes, such as when the horizontal and vertical screen is switched.
- onDrawFrame: Draw frames.
public class DemoRender implements GLSurfaceView.Renderer { @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { //The color used to clear the screen GLES30.glClearColor(1.0f, 0f, 0f, 0f); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { //Set palatable size GLES30.glViewport(0,0,width,height); } @Override public void onDrawFrame(GL10 gl) { //Use the color specified by glClearColor to erase the screen GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT); } }
This article combs the basic process of using opengles under android and the use of core classes, showing a pure color window.
Last
If you see it here and think it's a good article, give it a compliment! _____________ Welcome to comment and discuss! If you think it's worth improving, please leave me a message. We will seriously enquire, correct deficiencies, and regularly share free dry goods. Thank you!