Android OPENGLL Glsurfaceview Basic Implementation method

Source: Internet
Author: User

Glsurfaceview is a view that inherits to Surfaceview, and its embedded surface is dedicated to OpenGL rendering.

The following steps are implemented when you use it.

First, create a custom 1 class to inherit from Glsurfaceview, and create a constructor

Second, create a custom Class 2 implementation Glsurfaceview.renderer interface

Override Ondrawframe (GL10 gl), onsurfacechanged (GL10 gl, int width, int height), onsurfacecreated (GL10 gl, EGLConfig config) method

Define custom Class 2 in custom 1 and call Setrenderer (renderer) in the constructor of the custom Class 1 method for renderer settings

Create a custom Class 1 in the main ativity and set it as a view, in order to enable Glsurfaceview to synchronize with the activity, rewrite the Onpouse and Onresume methods of the activity, The Onpouse and Onresume methods of Glsurfaceview are called in the corresponding methods respectively.

Following a simple Glsurfaceview app, tap the screen to set the screen color based on screen coordinates. *******

Package Com.example.android_opengl;

Import Java.nio.ByteBuffer; Import Java.nio.ByteOrder; Import Java.nio.FloatBuffer;

Import Javax.microedition.khronos.egl.EGLConfig; Import Javax.microedition.khronos.opengles.GL; Import javax.microedition.khronos.opengles.GL10;

Import Android.content.Context; Import android.opengl.GLES20; Import Android.opengl.GLSurfaceView; Import Android.util.Log; Import android.view.MotionEvent;

public class Myglsurfaceview extends glsurfaceview{myglrender myrender;  Float vertexarray[]=new float[]{ -0.8f,-0.4f*1.732f,0.0f, 0.8f,-0.4f*1.732f,0.0f, 0.0f,0.4f*1.732f,0.0f};  Bytebuffer Vbb=bytebuffer.allocatedirect (vertexarray.length*4);  Floatbuffer FB;   Public Myglsurfaceview (Context context) {super (context);   TODO auto-generated Constructor stub vbb.order (Byteorder.nativeorder ());   Fb=vbb.asfloatbuffer ();   Fb.put (Vertexarray);      Fb.position (0); Myrender=new Myglrender ();

Setrenderer (Myrender); }

@Override public boolean ontouchevent (final Motionevent event) {//TODO auto-generated Method stub queueevent (new Runnable () {@Override public void run () {//TODO auto-generated Method stub Myrender.setcolor (event.ge         TX ()/GetWidth (), event.gety ()/GetHeight (), 1);   }   });  Return Super.ontouchevent (event); } Private class Myglrender implements renderer{

@Override public void Ondrawframe (GL10 gl) {//TODO auto-generated Method Stub Gl.glclearcolor (mred, Mgreen,mblue , 0.8f);//Set screen color 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 window   }

@Override public void onsurfacecreated (GL10 gl, EGLConfig config) {//TODO auto-generated method stub} Publ           IC void SetColor (float R, float g, float b) {mred = r%10; mGreen = g%10; mblue = b%10;         System.out.println ("R" +mred+ "G" +mgreen+ "B" +mblue);           } private float mred;           private float MGreen;                        private float Mblue; } }

Package com.example.opengl_huizhisanjiaoxing;

Import Android.os.Bundle; Import Android.provider.Settings.Global; Import android.app.Activity; Import Android.view.Menu;

public class Mainactivity extends Activity {

Myglsurfaceview Glview;   @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   Glview=new Myglsurfaceview (this);  Setcontentview (Glview); }

@Override protected void OnPause () {//TODO auto-generated Method Stub super.onpause ();  Glview.onpause ();   } @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();  Glview.onresume (); }

}

Android OPENGLL Glsurfaceview Basic Implementation method

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.