Enter how to handle
If you develop an interactive application (such as a game), you usually need to subclass the Glsurfaceview to get input events. Here's an example:
Java code:
- Package EoE. Cleartest;
- Import Javax.microedition.khronos.egl.EGLConfig;
- Import javax.microedition.khronos.opengles.GL10;
- Import android.app.Activity;
- Import Android.content.Context;
- Import Android.opengl.GLSurfaceView;
- Import Android.os.Bundle;
- Import android.view.MotionEvent;
- public class Clearactivity extends Activity
- {
- @Override
- protected void OnCreate (Bundle savedinstancestate)
- {
- Super.oncreate (savedinstancestate);
- Mglview = new Clearglsurfaceview (this);
- Setcontentview (Mglview);
- }
- @Override
- protected void OnPause ()
- {
- Super.onpause ();
- Mglview.onpause ();
- }
- @Override
- protected void Onresume ()
- {
- Super.onresume ();
- Mglview.onresume ();
- }
- Private Glsurfaceview Mglview;
- }
- Class Clearglsurfaceview extends Glsurfaceview
- {
- Public Clearglsurfaceview (context context)
- {
- Super (context);
- Mrenderer = new Clearrenderer ();
- Setrenderer (Mrenderer);
- }
- Public boolean ontouchevent (Final motionevent event)
- {
- Queueevent (New Runnable () {public void run ()
- {Mrenderer.setcolor (Event.getx ()/GetWidth (), event.gety ()/getheight () 1.0f);}); return true; } Clearrenderer Mrenderer; }
- Class Clearrenderer implements Glsurfaceview.renderer
- {
- public void onsurfacecreated (GL10 gl, EGLConfig config)
- {//Do nothing special.}
- public void onsurfacechanged (GL10 gl, int w, int h)
- {gl.glviewport (0, 0, W, h);}
- public void Ondrawframe (GL10 gl)
- {Gl.glclearcolor (mred, MGreen, Mblue, 1.0f);
- Gl.glclear (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit);
- }
- public void SetColor (float R, float g, float b)
- {
- mred = R; MGreen = g; Mblue = b; }
- private float mred;
- private float MGreen;
- private float Mblue;
- }
Copy Code
The app empties the screen in each frame. When you tap the screen, change the color of the screen. Here, Queueevent () is used in Clearglsurfaceview.ontouchevent (). The Queueevent () method is used to communicate between the UI thread and the render thread. It is also possible to use its Java threading technology, such as the Synchronized method, but queueevent the simplest thread communication method.
Other examples of Glsurfaceview
There are a number of examples in the Android SDK API Demo sample program:
Glsurfaceview
Kube
Translucent Glsurfaceview: Transparent background
Textured Triangle: Texture map
Sprite text: Write text on texture and display in 3D scene
Touch Rotate: Rotate 3D objects
Choose a surface
Glsurfaceview provides an interface to choose the type of surface. By default, Glsurfaceview uses a 16-bit RGB frame buffer with 16-bit depth. You can also choose according to your own needs, such as in the translucent Glsurfaceview example, you need an Alpha channel to achieve transparency. Glsurfaceview provides the Seteglsurfacechooser () method to select surface.
Select an RGB (565) 16-bit framebuffer with the following interface:
Seteglconfigchooser (Boolean needdepth) to customize red, green, blue, alpha, and depth, use the following interface:
Seteglconfigchooser (int redsize, int greensize,int bluesize, int alphasize,int depthsize, int stencilsize) are implemented using their own Eglconfigchooser, use the following interface:
Seteglconfigchooser (Eglconfigchooser configchooser) continuous rendering mode & notification rendering mode
Most 3D applications, such as games, simulations, and so on, are animated by continuous rendering, and some 3D applications are reactive (reactive), which tend to wait passively when the user has a motion to respond. For this application, continuous rendering of the screen is a waste of time. If you develop a reactive application, you can call the following method
Glsurfaceview.setrendermode (rendermode_when_dirty); stop rendering continuously. When calling
Glsurfaceview.requestrender (), the program renders the screen again.
Debug
The Glsurfaceview.setdebugflags () method can activate log or error detection, which can help debug OpenGL ES calls. When used specifically, call Glsurfaceview.setdebugflags () before calling Setrender () in the Glsurfaceview constructor. Here's an example:
Java code:
- Public Clearglsurfaceview (Context context)
- {
- Super (context);
- Turn on error-checking and Logging Setdebugflags (Debug_check_gl_error | Debug_log_gl_calls);
- Mrenderer = new Clearrenderer (); Setrenderer (Mrenderer);
Copy Code
Series of Android Glsurfaceview usage details (i) of the post link http://www.eoeandroid.com/thread-96857-1-1.html
Android Glsurfaceview Usage details (ii)