package com.util.helper;
import android.graphics.Bitmap;
import android.util.Log;
public class GifMaker {
private final String TAG=this.getClass().getName();
static
{
try {
System.loadLibrary("gifflen");
} catch (Exception e) {
e.printStackTrace();
//System.err.println("WARNING: Could not load library!");
Log.e("Giffle","WARNING: Could not load library!");
}
}
public GifMaker(){
framecount=0;
}
/**
* Init the gif file
* @param gifName name
* @param w width
* @param h height
* @param numColors colors
* @param quality
* @param frameDelay times
* @return
*/
public native int Init(String gifName, int w, int h, int numColors, int quality, int frameDelay);
public native void Close();
public native int AddFrame(int[] pixels);
public int add(String gifName,Bitmap b)
{
if(b==null){
Log.e(TAG,"add bitmap is null!!!!");
}
int width= b.getWidth();
int height= b.getHeight();
int delay=5;
if(framecount==0)
{
int ret=Init(gifName,width,height,256,100,delay);
if( ret!=0)
{
Log.e(TAG, "GifUtil init failed ret:"+ret);
return -1;
}
}
int pixels[]=new int[width*height];
b.getPixels(pixels, 0, width, 0, 0, width, height);
AddFrame(pixels);
pixels=null;
framecount++;
return framecount;
}
public int framecount;
/**
* encode the bitmaps to gif
* @param fileName
* @param bitmaps
* @param delay
*/
public void Encode(String fileName,Bitmap[] bitmaps,int delay)
{
if(bitmaps==null||bitmaps.length==0)
{
throw new NullPointerException("Bitmaps should have content!!!");
}
int width=bitmaps[0].getWidth();
int height=bitmaps[0].getHeight();
if(Init(fileName,width,height,256,100,delay)!=0)
{
Log.e(TAG, "GifUtil init failed");
return;
}
for(Bitmap bp:bitmaps)
{
int pixels[]=new int[width*height];
bp.getPixels(pixels, 0, width, 0, 0, width, height);
AddFrame(pixels);
}
Close();
}
}
gifflen 是