如何在Android中创建动画GIF(开发)

Sti*_*ter 45 animation android jpeg gif adt

我正在寻找一种在原生Android应用程序中创建动画GIF 的简单方法.源文件应为JPEG(来自相机或任何),输出应保存为设备上的GIF.

我不想知道如何播放动画或动画GIF文件.

要清楚:我想知道如何将单个图像逐帧放入"电影"中,然后将其另存为.gif文件.

例如,这个应用程序可以做我想做的事.

lif*_*ger 56

看到这个解决方案

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

这是这篇文章的Android版本.

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

要使用此类,下面是一个生成GIF字节数组的示例辅助方法.请注意,getBitmapArray()函数是一种立即返回图像适配器中所有Bitmap文件的方法.因此输入是一个适配器中的所有Bitmap文件,输出是一个字节数组,您可以写入该文件.

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();
}
Run Code Online (Sandbox Code Playgroud)

要使用此功能,请执行以下操作,然后将文件保存到SD卡中.

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

  • 这对我来说很慢。在我的连结5x上大约需要6秒钟的时间。每帧为1800x1000 (2认同)

Jat*_*wal 6

这可能对你有帮助.它是一个用于生成gif文件的类.

http://elliot.kroo.net/software/java/GifSequenceWriter/


Đăn*_*uân 5

它对我来说非常好,它可以快速创建文件和高质量的图像

//True for dither. Will need more memory and CPU
AnimatedGIFWriter writer = new AnimatedGIFWriter(true);
OutputStream os = new FileOutputStream("animated.gif");
Bitmap bitmap; // Grab the Bitmap whatever way you can
// Use -1 for both logical screen width and height to use the first frame dimension
writer.prepareForWrite(os, -1, -1)
writer.writeFrame(os, bitmap);
// Keep adding frame here
writer.finishWrite(os);
// And you are done!!!
Run Code Online (Sandbox Code Playgroud)

https://github.com/dragon66/android-gif-animated-writer