SoFunction
Updated on 2025-03-11

Android creates an Activity implementation method based on OpenGL's GLSurfaceView

This article describes the creation of an Activity implementation method based on OpenGL by Android. Share it for your reference, as follows:

Android provides two basic classes for us to create and manipulate graphics using the OpenGL ES API: GLSurfaceView and . Therefore, we need to understand these two classes first.

1. GLSurfaceView:

This is a view class, you can call the OpenGL API to draw graphics and manipulate objects, and the functions are similar to those of SurfaceView. We can create an instance of the GLSurfaceView class and add our own renderer. If we want to implement some touch screen operations ourselves, we must extend this class to implement touch listeners.

2.

This interface defines the methods required to draw graphics in an OpenGL GLSurfaceView. We must provide implementations for these interfaces in a separate class and attach it to the GLSurfaceView instance object using the() method.

The following methods we need to implement:

a) onSurfaceCreated(): The system calls it once when creating a GLSurfaceView. We can use it to set OpenGL environment variables, or initialize OpenGL graphic objects.

b) onDrawFrame(): The system calls this method every time the GLSurfaceView is redrawn. This method mainly completes the operation of drawing graphics.

c) onSurfaceChanged(): The system calls this method when the geometric properties of the GLSurfaceView change, including the size or direction of the device screen. For example, the system changes from upright to horizontal to invoke it. This method is mainly used to respond to changes in GLSurfaceView container.

Experimental steps

1. Add a new project

2. Add a new class myGLRenderer to implement the interface

The code is as follows:

public class myGLRenderer implements Renderer {
  @Override
  public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//Clear the cache  }
  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    (0, 0, width, height);//Set viewport  }
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    (0.5f, 0.5f, 0.5f, 1.0f); //Set clear color  }
}

3. Add a new class myGLSurfaceView, and the parent class is GLSurfaceView

The code is as follows:

public class myGLSurfaceView extends GLSurfaceView {
  public myGLSurfaceView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mrender = new myGLRenderer();
    setRenderer(mrender);
  }
  private myGLRenderer mrender;
}

4. The main program code is as follows:

public class HelloOpenGLActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    mGLSurfaceView = new myGLSurfaceView(this);
    setContentView(mGLSurfaceView);// Here we use mGLSurfaceView to replace the previously commonly used  }
  private myGLSurfaceView mGLSurfaceView;
}

In this way, we complete the application of using OpenGL to draw a gray background. Of course this is the most basic function. In the future, we will explore how to draw simple geometric figures using OpenGL.

For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.