English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the method of creating an Activity based on OpenGL GLSurfaceView in Android. Share it with everyone for reference, as follows:
Android provides two basic classes for us to use OpenGL ES API to create and manipulate graphics: GLSurfaceView and GLSurfaceView.Renderer. Therefore, we first need to understand these two classes.
1. GLSurfaceView:
This is a view class where you can draw graphics and manipulate objects using OpenGL API, similar to 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 a touch listener.
2. GLSurfaceView.Renderer
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 use the GLSurfaceView.setRenderer() method to attach it to the GLSurfaceView instance object.
We need to implement the following methods of GLSurfaceView.Renderer:
a) onSurfaceCreated(): The system calls this method once when creating GLSurfaceView. We can use it to set OpenGL environment variables or initialize OpenGL graphics objects.
b) onDrawFrame(): The system calls this method each time GLSurfaceView is redrawn. This method is mainly responsible for the operation of drawing graphics.
c) onSurfaceChanged(): The system calls this method when the geometric properties of GLSurfaceView change, including size or the direction of the device screen. For example, the system calls it when the screen changes from portrait to landscape. This method is mainly used to respond to changes in the GLSurfaceView container.
Experimental steps
1. Add a new item
2. Add a new class myGLRenderer, implementing the GLSurfaceView.Renderer interface
The code is as follows:
public class myGLRenderer implements Renderer { @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub gl.glClear(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 gl.glViewport(0, 0, width, height);//. Set the viewport } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //. Set the clear color } }
3. Add a new class myGLSurfaceView, with GLSurfaceView as the superclass
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) { super.onCreate(savedInstanceState); mGLSurfaceView = new myGLSurfaceView(this); setContentView(mGLSurfaceView);//Here we use mGLSurfaceView to replace the commonly used R.layout.main } private myGLSurfaceView mGLSurfaceView; }
In this way, we have completed the application of drawing a gray background using OpenGL. Of course, this is the most basic function. In the future, we will explore together how to draw simple geometric shapes using OpenGL.
Readers who are interested in more about Android-related content can check out the website's special topics: 'Summary of Activity Operation Skills in Android Programming', 'Summary of View Operation Skills in Android', 'Summary of SQLite Database Operation Skills in Android', 'Summary of JSON Format Data Operation Skills in Android', 'Summary of Database Operation Skills in Android', 'Summary of File Operation Skills in Android', 'Summary of SD Card Operation Methods in Android Programming', 'Introduction and Advanced Tutorial of Android Development', 'Summary of Resource Operation Skills in Android', and 'Summary of Android Widget Usage'.
I hope this article is helpful to everyone's Android program design.
Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and the website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liability. If you find any suspected copyright content, please send an email to notice#w.3Please send an email to codebox.com (replace # with @ when sending email) for reporting. Provide relevant evidence, and once verified, the website will immediately delete the suspected infringing content.