Android Gif Encoder
Given a sequence of bitmaps, encode them as an animated Gif. Based on several open-source libraries. Adapted for Android by Michael Lapadula.
- Import the files
GifEncoder.java
,LzwEncoder.java
, andNeuQuant.java
into your project - Note that Android's built-in Gallery app can't play animated gifs. You'll need a third-party app to view the gifs you generate. (Or write one yourself :) )
public void encodeGif(List<Bitmap> bitmaps, File outFile) {
try {
GifEncoder gifEncoder = new GifEncoder();
gifEncoder.start(outFile.getCanonicalPath());
gifEncoder.setDelay(500); // 500ms between frames
// Grab frames and encode them
Iterator<Bitmap> iter = bitmaps.iterator();
while (iter.hasNext()) {
Bitmap bitmap = iter.next();
gifEncoder.addFrame(bitmap);
}
// Make the gif
gifEncoder.finish();
// Add to gallery
Uri picUri = Uri.fromFile(outFile);
addToGallery(picUri);
} catch (IOException err) {
}
}