How to Create an Animated Gif from Jpegs in Android (Development)

How to create an animated GIF from JPEGs in Android (development)

See this solution.

https://github.com/nbadal/android-gif-encoder

It's an Android version of this post.

http://www.jappit.com/blog/2008/12/04/j2me-animated-gif-encoder/

To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.

public byte[] generateGIF() {
ArrayList<Bitmap> bitmaps = adapter.getBitmapArray();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
for (Bitmap bitmap : bitmaps) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}

To use this function, do the following then you can save the file into SDcard.

FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/generate_gif/test.gif");
outStream.write(generateGIF());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}

How to show Animated GIF image in android application?

After long Google search, I knew that there is no native support for the GIF images. There are no proper solutions for showing the animated gif in application. You can view This solution

to make the animated gif play in a layout.

Display Animated GIF

Android actually can decode and display animated GIFs, using android.graphics.Movie class.

This is not too much documented, but is in SDK Reference. Moreover, it is used in Samples in ApiDemos in BitmapDecode example with some animated flag.

Is there a way to create one Gif image from multiple images in Java?

Here you have an example of a class that creates an animated gif from different images:

Link

Edit: links seems to be dead. Anyway, just to be clear, this code was done by Elliot Kroo.

Edit 2: Thanks to @Marco13 for finding the WayBack Machine link. Updated the reference!

The class provides these methods:

class GifSequenceWriter {
public GifSequenceWriter(
ImageOutputStream outputStream,
int imageType,
int timeBetweenFramesMS,
boolean loopContinuously);

public void writeToSequence(RenderedImage img);

public void close();
}

And also a little example:

public static void main(String[] args) throws Exception {
if (args.length > 1) {
// grab the output image type from the first image in the sequence
BufferedImage firstImage = ImageIO.read(new File(args[0]));

// create a new BufferedOutputStream with the last argument
ImageOutputStream output =
new FileImageOutputStream(new File(args[args.length - 1]));

// create a gif sequence with the type of the first image, 1 second
// between frames, which loops continuously
GifSequenceWriter writer =
new GifSequenceWriter(output, firstImage.getType(), 1, false);

// write out the first image to our sequence...
writer.writeToSequence(firstImage);
for(int i=1; i<args.length-1; i++) {
BufferedImage nextImage = ImageIO.read(new File(args[i]));
writer.writeToSequence(nextImage);
}

writer.close();
output.close();
} else {
System.out.println(
"Usage: java GifSequenceWriter [list of gif files] [output file]");
}
}

Props to Elliot Kroo for this code.

Use gif as jpg in project

You can declare it, but it won't be animated. If you want it to be, you can use webview (not good solution) and VideoView. You can find some help here: Display Animated GIF. If you really want to be in a ImageView check this out: Playing an animated GIF image file in imageview. Whatever you decide, you can find a lot of useful stuff here: http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/ . Hope this helps.



Related Topics



Leave a reply



Submit