Android:glsurfaceview drawing bitmap picture and the effect of glviewport adjustment

Source: Internet
Author: User

First look at how the Glsurfaceview is drawn. As described in the Android development documentation, we need a new Glsurfaceview object, and then set up an object that implements the renderer interface, we need to write a Myrender class, implement the Renderer method, and so on. Let's look at the writing of activity first. It does not need a layout file, we will glsurfaceview the object as an activity Contentview. As follows:

Package Net.mobctrl.glsurfaceviewbmp;import Java.util.random;import Android.annotation.suppresslint;import Android.app.activity;import Android.opengl.glsurfaceview;import Android.os.bundle;import android.view.windowmanager;/** * * @author Zheng Haibo * @web http://www.mobctrl.net * */public class mainactivity Extend s Activity {private glsurfaceview Glview;//Use glsurfaceviewrandom rnd = new Random (), @SuppressLint ("Newapi") @Overridep rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); SetTheme (Android. R.style.theme_translucent_notitlebar); GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Glview = new Glsurfaceview (this); Allocate a Glsurfaceviewglview.setrenderer (new Myglrenderer (this)); Use a custom Rendererglview.getalpha (); This.setcontentview (Glview); } @Overrideprotected void OnPause () {super.onpause (); Glview.onpause ();} @Overrideprotected void Onresume () {super.onresume (); Glview.onresume (); ANDROID.PROVIDER.SETTINGS.SYSTEM.PUtint (This.getcontentresolver (), Android.provider.settings.system.screen_brightness,rnd.nextint (256));}} 

Below, we mainly look at the Myglrender class, it implements the renderer interface, specifically as follows:

Package Net.mobctrl.glsurfaceviewbmp;import Java.util.random;import javax.microedition.khronos.opengles.GL10; Import Android.content.context;import android.opengl.glsurfaceview.renderer;import android.opengl.glu;/** * @date October 20, 2014 PM 2:49:17 * @author Zheng Haibo * @Description: TODO */public class Myglrenderer implements Renderer {Context Context Application ' s Contextrandom r = new Random ();//private Square square;private glbitmap glbitmap;private int width = 0;PR ivate int height = 0;private long frameseq = 0;private int viewportoffset = 0;private int maxoffset = 400;public Myglrende RER (context context) {This.context = Context;//square = New Square (); glbitmap = new Glbitmap ();} @Overridepublic void onsurfacechanged (GL10 gl, int width, int height) {if (height = = 0) {//Prevent A Divide by Zero byhe ight = 1; Making Height Equal one}this.width = width;this.height = Height;gl.glviewport (0, 0, width, Height); Reset the//current//Viewportgl.glmatrixmode (gl10.gl_projection); //Select the Projection matrixgl.glloadidentity (); Reset the Projection matrix//Calculate the Aspect Ratio of the Windowglu.gluperspective (GL, 45.0f, (float) width/(fl Oat) height, 0.1f,100.0f); Gl.glmatrixmode (Gl10.gl_modelview); Select the Modelview matrixgl.glloadidentity ();} /** * Once every 16ms */@Overridepublic void Ondrawframe (GL10 gl) {gl.glclear (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit);//Reset the Modelview matrixgl.glloadidentity (); Gl.gltranslatef (0.0f, 0.0f, -5.0f); Move 5 units into the is//the same as moving the camera 5//units away//Square.draw (GL); Glbitmap.draw (GL); Chan Geglviewport (GL);} /** * By changing the view of GL * * @param gl */private void Changeglviewport (GL10 gl) {System.out.println ("time=" + system.currenttime Millis ()); frameseq++;viewportoffset++;//the//currentif (frameseq% 100 = = 0) {//Every 100 frames, reset gl.glviewport (0, 0, width, hei ght); viewportoffset = 0;} else {int k = 4;gl.glviewport (-maxoffset + viewportoffset * k,-maxoffset+ Viewportoffset * k, This.width-viewportoffset * 2 * k + maxoffset * 2, This.height-viewportoffset * 2 * k + maxoffset * 2);}} @Overridepublic void onsurfacecreated (GL10 gl,javax.microedition.khronos.egl.eglconfig arg1) { Glbitmap.loadgltexture (GL, this.context); gl.glenable (gl10.gl_texture_2d); Enable Texture Mapping (NEW) Gl.glshademodel (Gl10.gl_smooth); Enable Smooth Shadinggl.glclearcolor (0.0f, 0.0f, 0.0f, 0.5f); Black BACKGROUNDGL.GLCLEARDEPTHF (1.0f); Depth Buffer setupgl.glenable (gl10.gl_depth_test); Enables Depth Testinggl.gldepthfunc (gl10.gl_lequal); The Type of Depth testing to Dogl.glhint (Gl10.gl_perspective_correction_hint, gl10.gl_nicest);}}

Note that there are three main ways to implement this:onsurfacecreated,onsurfacechanged and Ondrawframe, of which the key is the Ondrawframe method, its drawing mechanism is: 1s of time, the Ondrawframe method will be called 60 times, approximately every 16ms call. Let's look at the Glbitmap object inside, Changeglviewport the method later.

Package Net.mobctrl.glsurfaceviewbmp;import Java.nio.bytebuffer;import Java.nio.byteorder;import Java.nio.floatbuffer;import Javax.microedition.khronos.opengles.gl10;import NET.MOBCTRL.GLSURFACEVIEWBMP.R; Import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import android.opengl.glutils;/** * @date October 20, 2014 PM 3:09:33 * @author Zheng Haibo * @Description: TODO */public class Glbitma p {private Floatbuffer texturebuffer;//buffer holding the texture coordinatesprivate float texture[] = {//Mapping coord Inates for the vertices0.0f, 1.0f,//Top Left (V2) 0.0f, 0.0f,//Bottom Left (V1) 1.0f, 1.0f,//Top Right (V4) 1.0f, 0.0f Bottom right (V3)};p rivate Floatbuffer vertexbuffer;  Buffer holding the verticesprivate float vertices[] = { -1.0f, -1.0f, 0.0f,//V1-bottom left-1.0f, 1.0f, 0.0f,//V2 -Top left1.0f, -1.0f, 0.0f,//V3-bottom right1.0f, 1.0f, 0.0f//V4-top right};p ublic Glbitmap () {Bytebuffer Bytebu Ffer = Bytebuffer.allocAtedirect (Vertices.length * 4); Bytebuffer.order (Byteorder.nativeorder ()); vertexbuffer = Bytebuffer.asfloatbuffer () ; Vertexbuffer.put (vertices); vertexbuffer.position (0); bytebuffer = Bytebuffer.allocatedirect (Texture.length * 4); Bytebuffer.order (Byteorder.nativeorder ()); texturebuffer = Bytebuffer.asfloatbuffer (); texturebuffer.put (texture); Texturebuffer.position (0);} /** the texture pointer */private int[] textures = new Int[1];p ublic void Loadgltexture (GL10 GL, Context context) {//load ing texturebitmap bitmap = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.plane_image);//Generate One texture pointergl.glgentextures (1, textures, 0);//. and bind it to our arraygl.glbindtexture (gl10.gl_texture_2d, TE Xtures[0]);//create Nearest filtered Texturegl.gltexparameterf (gl10.gl_texture_2d, Gl10.gl_texture_min_filter,gl10 . gl_nearest); Gl.gltexparameterf (gl10.gl_texture_2d, gl10.gl_texture_mag_filter,gl10.gl_linear);//Use Android Glutils to specify a two-dimensional texture image from//our bitmapglutils.teximage2d (gl10.gl_texture_2d, 0, bitmap, 0);//clean upbitmap.recycle ();} public void Draw (GL10 gl) {/Bind the previously generated texturegl.glbindtexture (gl10.gl_texture_2d, textures[0]);//P Oint to our buffersgl.glenableclientstate (Gl10.gl_vertex_array); Gl.glenableclientstate (GL10.GL_TEXTURE_COORD_ ARRAY);//Set the face Rotationgl.glfrontface (GL10.GL_CW);//point to our vertex buffergl.glvertexpointer (3, Gl10.gl_ FLOAT, 0, VertexBuffer); Gl.gltexcoordpointer (2, gl10.gl_float, 0, Texturebuffer);//Draw the vertices as triangle STRIPGL . Gldrawarrays (Gl10.gl_triangle_strip, 0, VERTICES.LENGTH/3);//Disable the client state before Leavinggl.gldisableclientstate (Gl10.gl_vertex_array); gl.gldisableclientstate (Gl10.gl_texture_coord_array);}}


As you can see from Glbitmap, we first need to load a picture resource and then generate texture. Here, in particular, changeglviewport this method, if you remove this method in Myrender, run the result is, Glsurfaceview draw a picture. After adding this method, it is periodic to draw a "animation" from far to near. Note that this animation does not change the image itself, but does not adjust the angle of view produced, so the efficiency is very high. Its principle, like you take a camera, to shoot a picture on the wall, the camera closer to the picture, the picture is magnified, and the wall itself does not change.


-------------------------------------------------------------------

more communication, Android Development Alliance QQ Group: 272209595


Android:glsurfaceview drawing bitmap picture and the effect of glviewport adjustment

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.