Android Official Development Document Training Series: Environment configuration of OpenGL drawing

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/graphics/opengl/index.html

Introduction

The Android framework layer provides a number of standard tools for creating beautiful, functional UIs. However, if you want to control the drawing of the screen in more ways, or draw in a three-dimensional drawing, then you need to use other tools. The OpenGL ES API provided by the Android framework provides us with a range of tools that can be used to show some high-end atmosphere, the most imaginative graphics, as long as you can imagine, then it can be done. In addition, it benefits from the GPU acceleration capabilities offered by many devices.

This lesson discusses the basics of OpenGL development: Setting up the environment, drawing objects, moving drawing elements, and responding to touch events.

The OpenGL ES 2.0 API is used in the following example code, which is recommended for use on current Android devices. For more information about the version of OpenGL ES, see the OpenGL Development Guide.

Note : be careful not to confuse the OpenGL ES 1.x API with the OpenGL ES 2.0 API! These two versions of the API are not interchangeable between the use, if you want to use, then only one result, is a tragedy.

Building the relevant environment for OpenGL ES

In order to use OpenGL es in Android apps, you must create a view container for the graphics area that OpenGL ES will draw. One of the implementation methods is to achieve Glsurfaceview and glsurfaceview.renderer. Where Glsurfaceview is the view container that draws graphics in OpenGL. The glsurfaceview.renderer is used to control what should be drawn in the view just now. For more information about these classes, see the OpenGL ES Development Guide.

Glsurfaceview is the only way to integrate OpenGL ES into your application. For full-screen or near-full-screen graphics View,glsurfaceview is the most appropriate choice. Developers should use Textureview if they need to integrate OpenGL ES into a small area. In addition to using Surfaceview, this may require considerable code to be implemented.

This lesson will show you the most provincial code to implement Glsurfaceview and Glsurfaceview.renderer.

Declaring the use of OpenGL es in the manifest file

In order to use the OpenGL ES 2.0 API, you need to add the following declaration in the manifest file:

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

If the application uses texture compression, you also need to declare the compression format used by the app, so that the app can only be installed on supported devices.

<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" /><supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />

For more information about texture compression formats, see the OpenGL Development Guide.

Create an activity for OpenGL ES

As with common applications, OpenGL es also requires a user interface. The main difference is that ordinary applications simply need to put the layout into activity. In applications that use OpenGL ES, you need to add Glsurfaceview in addition to basic controls such as normal TextView.

The following code shows the most basic implementation of using Glsurfaceview:

public  class  opengles20activity  extends  activity  {     Private  Glsurfaceview Mglview;     @Override  public  void  oncreate  (Bundle savedinstancestate)        {super . OnCreate (savedinstancestate);        //Create a Glsurfaceview instance and set it         //as the Contentview for this Activity.         Mglview = new  myglsurfaceview (this );    Setcontentview (Mglview); }}

Note: OpenGL ES 2.0 needs to be run on Android 2.2 and above, make sure the minimum version of the application is on top of that.

Building Glsurfaceview Objects

Glsurfaceview is a special area that allows users to draw OpenGL ES graphics. It does not need to do too much in itself. In fact, the drawing of graphs is controlled by Glsurfaceview.renderer. In fact, you might want to try not to inherit this class, but instead create a native Glsurfaceview instance directly, and don't do that. You need to inherit this class to capture touch events, and the relevant information is involved in the responding to touch events course.

The code is inherently glsurfaceview, so it's common practice to create an inner class in activity that uses this object in order to quickly implement it:

class MyGLSurfaceView extends GLSurfaceView {    privatefinal MyGLRenderer mRenderer;    publicMyGLSurfaceView(Context context){        super(context);        // Create an OpenGL ES 2.0 context        setEGLContextClientVersion(2);        new MyGLRenderer();        // Set the Renderer for drawing on the GLSurfaceView        setRenderer(mRenderer);    }}

The additional option for Glsurfaceview is to set its rendering mode:

// Render the view only when there is a change in the drawing datasetRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

This option prevents the Glsurfaceview frame from being redrawn until the Requestrender () method is called. This can improve the efficiency of your application.

Building the Renderer class

Class Glsurfaceview.renderer are really interesting places. This class can control what is drawn on the Glsurfaceview. It has 3 methods inside, and these 3 methods are called by the Android system to calculate how to draw on Glsurfaceview:

    • Onsurfacecreated () is called once when setting up the OpenGL ES environment.
    • Each drawing of the ondrawframe () view is called.
    • Onsurfacechanged () is called when the structure of the view changes, such as when the screen orientation of the device has changed.

Here is the most basic implementation of the OpenGL ES renderer, which simply draws a black background in Glsurfaceview:

 Public  class myglrenderer implements Glsurfaceview. Renderer {     Public void onsurfacecreated(GL10 unused, EGLConfig config) {//Set the background frame colorGles20.glclearcolor (0.0F0.0F0.0F1.0f); } Public void Ondrawframe(GL10 unused) {//Redraw background colorGles20.glclear (Gles20.gl_color_buffer_bit); } Public void onsurfacechanged(GL10 unused,intWidthintHeight) {Gles20.glviewport (0,0, width, height); }}

The above is all the work to be done. The above code uses OpenGL to draw a black background. While this code doesn't do anything interesting, by creating these classes you can lay the groundwork for drawing graphics through OpenGL.

Note: You may wonder why these methods will have a parameter called GL10 when using the OPENGGL ES 2.0 API. These re-used signature methods in the 2.0 API are designed to keep the Android framework code simple.

If you're familiar with the OpenGL ES API, you can now set up the OpenGL ES environment and start drawing. Anyway, if you want to get more help with getting started with OpenGL, you can look at some of the tips in the next section.

Android Official Development Document Training Series: Environment configuration of OpenGL drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.