加载gif之上层调用

来源:互联网 发布:五轴仿真软件 编辑:程序博客网 时间:2024/06/06 10:56

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 ;
 /** 纹理数据的句柄. */ 
 private int mTextureDataHandle;
 final int[] textureHandle = new int[1];
 
 
 static {
  System.loadLibrary("glgif");
 }

 
 public myGLRenderer() {
  super();
  // TODO Auto-generated constructor stub
  rectData = new float[]{
    250.0f,250.0f, //右上顶点
    250.0f,-250.0f, //右下顶点
    -250.0f,250.0f, //左上顶点
    -250.0f,-250.0f,//左下顶点
  };
  textureData =  new float[]{
    250.0f,250.0f, //右上顶点
    250.0f,-250.0f, //右下顶点
    -250.0f,250.0f, //左上顶点
    -250.0f,-250.0f, //左下顶点
  };
  
 }
 
 
 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);//清空缓存
  
  mTextureDataHandle = loadTexture(gl);
  
  
  gl.glEnableClientState(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 = 100.0f;
        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.glEnable(GL11.GL_DITHER);
        gl.glDisable(GL11.GL_LIGHTING);
       
        // Enable textures.启用纹理贴图
        gl.glEnable(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 into
        // one-time
        // initialization. This section may need to be folded into drawMixed2D()
        // if multitexture
        // is used for other effects.

        // Enable Vertex Arrays
        gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
     

        // Enable depth test.
        gl.glEnable(GL11.GL_DEPTH_TEST);
        gl.glDepthFunc(GL11.GL_LEQUAL);

       
        // Set the blend function for premultiplied alpha.
        gl.glEnable(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]);            
    
   //设置纹理映射的属性   
   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);  
     //生成纹理映射   
   GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);  
     //释放bitmap资源   
   bitmap.recycle();        
  }        
  
  if (textureHandle[0] == 0)     
  {         
   throw new RuntimeException("Error loading texture.");     
  }        
  return textureHandle[0]; 
  
 } 
 public Buffer intBuffer(int[] data)
 {       
  IntBuffer intBuffer ;      
  ByteBuffer bbuffer = ByteBuffer.allocateDirect(data.length * 4); 
  //int和float均占用4字节    
  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;      
 }

 
}

原创粉丝点击