Android Glsurfaceview to create an activity implementation method based on OpenGL _android

Source: Internet
Author: User
Tags sqlite database stub

An example of this article is the Android Glsurfaceview creation of an activity implementation method based on OpenGL. Share to everyone for your reference, specific as follows:

Android offers two basic classes that let us use the OpenGL ES API to create and manipulate graphics: Glsurfaceview and Glsurfaceview.renderer. So we first need to understand these two classes.

1. Glsurfaceview:

This is a view class, and you can invoke the OpenGL API to draw graphics and manipulate objects on it, similar in function to Surfaceview. We can create an instance of the Glsurfaceview class and add our own renderer. If we're going to implement some touch-screen operations on our own, we have to extend this class to implement the touch listener.

2. Glsurfaceview.renderer

This interface defines the method needed to draw a graphic 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 Glsurfaceview.setrenderer () method.

We need to implement the following methods of Glsurfaceview.renderer:

A) onsurfacecreated (): The system calls it once when creating Glsurfaceview. We can use it to set the environment variable for OpenGL or to initialize the graphics object of OpenGL.

b) Ondrawframe (): The system calls this method each 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 the direction of the device screen. For example, the system changes from upright to horizontal on the screen to make it call. This method is mainly used to respond to changes in the Glsurfaceview container.

Experiment steps

1. Add New Item

2. Add new class Myglrenderer, implement Glsurfaceview.renderer interface

The code is as follows:

public class Myglrenderer implements Renderer {
  @Override public
  void Ondrawframe (GL10 gl) {
    //TODO Auto-gen erated method Stub
    gl.glclear (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit)/Empty cache
  }
  @Override public
  void onsurfacechanged (GL10 GL, int width, int height) {
    //TODO auto-generated method stub
    gl.glviewport (0, 0, width, height);/set 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 purge Color
  }
}

3. Add a new class Myglsurfaceview, the parent class is Glsurfaceview

The code is as follows:

public class Myglsurfaceview extends Glsurfaceview {public
  Myglsurfaceview (context) {
    super);
    //TODO auto-generated constructor stub
    mrender = new Myglrenderer ();
    Setrenderer (Mrender);
  }
  Private Myglrenderer mrender;
}

4. The main program code is as follows:

The public class Helloopenglactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '
  --the
  @Override c13/>public void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Mglsurfaceview = new Myglsurfaceview (this);
    Setcontentview (Mglsurfaceview)//Here we use Mglsurfaceview to replace the previously used R.layout.main
  }
  private Myglsurfaceview Mglsurfaceview;
}

This completes the application using OpenGL to draw a gray background. Of course, this is the most basic function. Later we'll explore how to use OpenGL to draw simple geometries.

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

Related Article

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.