(Welcome to join the android technology exchange QQ group: 209796692)
Create an OpenGL ES Environment
To use opengles painting in your Android app, you must create a view as a container. The most direct way isGLSurfaceView
AndGLSurfaceView.Renderer
A class is derived.
GLSurfaceView
As the container where OpenGL is drawn, the actual drawing action is inGLSurfaceView.Renderer
.
Use
GLSurfaceView
It is almost the only way to integrate opengles into your application. It is the best choice for a full screen or near-full screen graphicsview. You can considerTextureView
. If you are abnormal, you can use
SurfaceView
Create
Opengles view.
This tutorial demonstrates how to complete a minimum-implemented opengles2.0 application.
Declare the use of opengles in manifest
To use opengles 2.0 API, you must add the following statement to your manifest:
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
If your application uses the texture compression function, you must declare what compression formats the device needs to support:
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" /><supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
Create an activity for opengles graphics
This activity is no different from the activity of other types of applications. To put it in a different view, you only need to put it in the Layout View of the activity.GLSurfaceView
.
The following code demonstrates how to useGLSurfaceView
As the main view
Acitivity
Minimum Code implementation:
Public class opengles20 extends activity {private glsurfaceview mglview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // create a glsurfaceview instance and set it to the contentview of the activity. mglview = new myglsurfaceview (this); setcontentview (mglview );}}
Note:OpenGL ES 2.0 requires Android (API Level 8) and later versions.
Creates a glsurfaceview object.
GLSurfaceView
In fact, do not need to do too much work, the actual drawing tasks areGLSurfaceView.Renderer
In progress
. So
GLSurfaceView
There are very few codes in, and you can even use them directly.
GLSurfaceView
However, do not. Because you need to extend this class to respond to touch events.
Extension
GLSurfaceView
The class can be written like this:
Class myglsurfaceview extends glsurfaceview {public myglsurfaceview (context) {super (context); // set Renderer to glsurfaceview setrenderer (New myrenderer ());}}
When using opengles 2.0, you mustGLSurfaceView
The constructor calls another function, which indicates that you will use the 2.0 API:
// Create an OpenGL ES 2.0 contextseteglcontextclientversion (2 );
Another one can add yourGLSurfaceView
The optional operation is to set the render mode to draw a view only when the data to be drawn changes. UseGLSurfaceView.RENDERMODE_WHEN_DIRTY
:
// Viewsetrendermode (glsurfaceview. rendermode_when_dirty) is drawn only when the drawing data changes );
This setting prevents plotting.GLSurfaceView
Until you callrequestRender()
.
Build a Renderer class
This type of control directionGLSurfaceView
. It has three methods called by the Android system to calculateGLSurfaceView
What to draw and how to draw.
onSurfaceCreated()
-It is called only once to set the opengles environment of the view.
onDrawFrame()
-The view is called every time it is re-painted.
onSurfaceChanged()
-If the number and shape of the view change, call the function, for example, when the portrait screen changes to a landscape screen.
The following is the most basic implementation of an opengles Renderer.GLSurfaceView
Upper
Painted a gray background:
Public class mygl20renderer implements glsurfaceview. renderer {public void onsurfacecreated (gl10 unused, eglconfig config) {// set the background color gles?glclearcolor (0.5f, 0.5f, 0.5f, 1.0f);} public void ondrawframe (gl10 unused) {// re-paint the background color gles‑glclear (gles‑gl _ color_buffer_bit);} public void onsurfacechanged (gl10 unused, int width, int height) {gles‑glviewport (0, 0, width, height );}}
The above is what we need to do! The above Code creates a simple Android Application, which uses OpenGL to display a gray screen. However, this code does not do anything interesting, but is ready to use OpenGL for plotting.
Note:You don't understand why these methods all haveGL10
But you are using openggles 2.0 APIs. This is actually done to make the android framework simple and compatible with various opengles versions.
If you are familiar with the opengles API, you can start plotting now. However, if you are familiar with it, continue to the next chapter.
Next lecture