Upper-layer call for GIF Loading

Source: Internet
Author: User

Package com. ZTE. liaoxy;

Import java. NiO. buffer;
Import java. NiO. bytebuffer;
Import java. NiO. byteorder;
Import java. NiO. floatbuffer;
Import java. NiO. intbuffer;

Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. opengles. gl10;
Import javax. microedition. khronos. opengles. gl11;

Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmap. config;
Import Android. Graphics. paint;
Import Android. OpenGL. glsurfaceview. Renderer;
Import Android. OpenGL. Glu;
Import Android. OpenGL. glutils;
Import Android. util. log;

Public class myglrenderer implements Renderer {

Private long mgif = 0;
Private int width;
Private int heigth;
Floatbuffer rectdatabuffer;
Floatbuffer texturedatabuffer;
Float [] rectdata;
Float [] texturedata;
Bitmap bitmap;
/** Texture data handle .*/
Private int mtexturedatahandle;
Final int [] texturehandle = new int [1];
 
 
Static {
System. loadlibrary ("glgif ");
}

 
Public myglrenderer (){
Super ();
// Todo auto-generated constructor stub
Rectdata = new float [] {
250f, 250f, // top right Vertex
2500000f,-2500000f, // bottom right Vertex
-250f, 250f, // top left Vertex
-2500000f,-2500000f, // left bottom Vertex
};
Texturedata = new float [] {
250f, 250f, // top right Vertex
2500000f,-2500000f, // bottom right Vertex
-250f, 250f, // top left Vertex
-2500000f,-2500000f, // left bottom Vertex
};

}
 
 
Protected native long nativecreategif (string path );
Protected native void nativedeletegif (long gifid );
Protected native void nativecopytobitmap (Bitmap bitmap, long gifid, int left, int top );
Protected native int getnativegifwidth (long gifid );
Protected native int getnativegifheigth (long gifid );
@ Override
Public void ondrawframe (gl10 GL ){
// Todo auto-generated method stub
Gl. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // clear the cache

Mtexturedatahandle = loadtexture (GL );


Gl. glableclientstate (gl10.gl _ texture_coord_array );
// Gl. glmatrixmode (gl10.gl _ modelview );
// Gl. glloadidentity ();
// Gl. gltranslatef (0.0f, 0.0f,-6.0f );
Gl. glvertexpointer (2, gl10.gl _ float, 0, floatbuffer (rectdata ));
Gl. gltexcoordpointer (2, gl10.gl _ float, 0, floatbuffer (texturedata ));
Gl. glbindtexture (gl10.gl _ texture_2d, mtexturedatahandle );

Gl. gldrawarrays (gl10.gl _ triangle_strip, 0, 4 );

Gl. gldeletetextures (1, texturehandle, 0 );
Gl. glfinish ();


}
 
Public void loadgif (string path ){
If (null = path ){
Throw new illegalargumentexception ("path is null ");
}
Log. I ("gifview", "=== come in loadgif ");
This. replacegifid (this. nativecreategif (PATH ));
}
Protected synchronized void replacegifid (long gifid ){
Log. I ("gifview", "gifid =" + gifid + "This. mgif =" + this. mgif );
If (0! = This. mgif ){
This. nativedeletegif (this. mgif );
}
This. mgif = gifid;
This. width = This. getnativegifwidth (this. mgif );
This. heigth = This. getnativegifheigth (this. mgif );
}
@ Override
Public void onsurfacechanged (gl10 GL, int width, int height ){
// Todo auto-generated method stub
Final float znear = 0.1f;
Final float zfar = 100366f;
Gl. glviewport (0, 0, width, height );
Gl. glmatrixmode (gl11.gl _ projection );
Gl. glloadidentity ();
Glu. gluperspective (GL, 45.0f, (float) width/height, znear, zfar );
Gl. glmatrixmode (gl11.gl _ modelview );
}

@ Override
Public void onsurfacecreated (gl10 GL, eglconfig config ){
// Todo auto-generated method stub

Loadgif ("Data/APP/girl.gif ");
Gl. glable (gl11.gl _ dither );
Gl. gldisable (gl11.gl _ lighting );

// Enable textures. Enable texture Paster
Gl. glable (gl11.gl _ texture_2d );
Gl. gltexenvf (gl11.gl _ texture_env, gl11.gl _ texture_env_color, gl11.gl _ replace );

// Set up state for multitexture operations. Since multitexture is
// Currently used
// Only for layered crossfades the needed State can be factored out
// One-time
// Initialization. This section may need to be folded into drawmixed2d ()
// If multitexture
// Is used for other effects.

// Enable vertex Arrays
Gl. glableclientstate (gl11.gl _ vertex_array );

// Enable depth test.
Gl. glable (gl11.gl _ depth_test );
Gl. gldepthfunc (gl11.gl _ lequal );


// Set the blend function for premultiplied Alpha.
Gl. glable (gl10.gl _ blend );
Gl. glblendfunc (gl11.gl _ src_alpha, gl11.gl _ one_minus_src_alpha );

// Set the background color.
Gl. glclearcolor (0.0f, 0.0f, 0.0f, 0.0f );
Gl. glclear (gl11.gl _ color_buffer_bit );

}
 
Public int loadtexture (gl10 GL)
{
Gl. glgentextures (1, texturehandle, 0 );
Final long gifid = This. mgif;
If (texturehandle [0]! = 0 & gifid! = 0)
{


Bitmap = bitmap. createbitmap (this. Width, this. heigth, config. argb_8888 );

This. nativecopytobitmap (bitmap, gifid, 50, 50 );

// Bind to the texture in OpenGL
Gl. glbindtexture (gl10.gl _ texture_2d, texturehandle [0]);

// Set texture ing attributes
Gl. 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 _ nearest );
// Generate texture ing
Glutils. teximage2d (gl10.gl _ texture_2d, 0, bitmap, 0 );
// Release bitmap Resources
Bitmap. Recycle ();
}

If (texturehandle [0] = 0)
{
Throw new runtimeexception ("error loading texture .");
}
Return texturehandle [0];

}
Public buffer intbuffer (INT [] data)
{
Intbuffer;
Bytebuffer bbuffer = bytebuffer. allocatedirect (data. length * 4 );
// Both int and float occupy 4 bytes
Bbuffer. Order (byteorder. nativeorder ());
Intbuffer = bbuffer. asintbuffer ();
Intbuffer. Put (data );
Intbuffer. Position (0 );
Return intbuffer;
}
Public buffer floatbuffer (float [] data)
{
Floatbuffer fbuffer;
Bytebuffer bbuffer = bytebuffer. allocatedirect (data. length * 4 );
Bbuffer. Order (byteorder. nativeorder ());
Fbuffer = bbuffer. asfloatbuffer ();
Fbuffer. Put (data );
Fbuffer. Position (0 );
Return fbuffer;
}

 
}

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.