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

时间:2022-10-25 21:16:18

I am looking for a simple way to create an animated GIF in a native Android application. The source files should be JPEG (from camera or what ever) and the output should be saved as GIF on the device.

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

I do not want to know how to play animations or animated GIF files.

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

To be clear: I want to know how to put single images frame by frame into a "movie" and then save it as a .gif file.

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

e.g. This App can do want I want to do.

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

6 个解决方案

#1


53  

See this solution.

看到这个解决方案

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

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

It's an Android version of this post.

这是这篇文章的Android版本。

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

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.

要使用此类,下面是一个生成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();
}

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

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

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

#2


6  

This might help you. It's a class which is used to generate gif files.

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

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

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

#3


3  

If you only want to display these bitmaps like an animated gif, you can create an AnimatedDrawable using this code:

如果您只想像动画gif那样显示这些位图,可以使用以下代码创建AnimatedDrawable:

AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 10);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 50);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 30);
animation.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.imageView);
imageAnim.setImageDrawable(animation);

// start the animation!
animation.start();

#4


2  

It did very well for me, It create file fast and good quality images

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

//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!!!

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

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

#5


1  

Solution 1

解决方案1

Have you seen this article? ..... it's with source code..

你看过这篇文章吗? .....这是源代码..

As I know, I think android won't play gif animation image. You should use webview that will play gif animation..

据我所知,我认为android不会播放gif动画图片。您应该使用将播放gif动画的webview ..

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

and if you can..

如果你能......

http://androidosbeginning.blogspot.in/2010/09/gif-animation-in-android.html

http://androidosbeginning.blogspot.in/2010/09/gif-animation-in-android.html

Solution 2

解决方案2

Android provides Drawable Animation.

Android提供可绘制动画。

#6


0  

Gif is basically a set of images with a constant time delay in frames. You can not directly play gif in android. I've worked lot on playing gif animation in android.

Gif基本上是一组在帧中具有恒定时间延迟的图像。你无法在android中直接玩gif。我在android上玩gif动画很多。

  • When playing gif we also handle all calls of playing frames by our self. So this is not a best approach

    在玩gif时,我们也可以自己处理所有玩框架的调用。所以这不是最好的方法

  • If you have set of images just play them by a delay in frames. That would be the all you want.

    如果您有一组图像,只需按帧延迟播放即可。这就是你想要的一切。

  • You can not make gif in application by native code as well. There are some drawbacks as well for using gif format. Which I had faced in my game.

    您也不能通过本机代码在应用程序中生成gif。使用gif格式也有一些缺点。这是我在游戏中面临的问题。

  • If you have set of drawables you can use AnimationDrawable for showing aanimation like gif as well. It can also set the any View.

    如果你有一组drawable,你可以使用AnimationDrawable来显示像gif一样的动画。它还可以设置任何视图。

I had also made a custom view to play gif animations. First I load gif and convert it into InputStream then I pass it to my custom view class to play the gif.

我也制作了一个自定义视图来播放gif动画。首先我加载gif并将其转换为InputStream然后我将它传递给我的自定义视图类来播放gif。

 public class GifWebView extends View {
    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;
    private boolean play;

public GifWebView(Context context, InputStream stream) {
    super(context);
    mStream = stream;
    setPlay(true);
    mMovie = Movie.decodeStream(mStream);
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    final long now = SystemClock.uptimeMillis();

    if (mMoviestart == 0) {
        mMoviestart = now;
    }
    final int relTime = (int) ((now - mMoviestart) % mMovie.duration());

    mMovie.setTime(relTime);
    mMovie.draw(canvas, 20, 20);
    if (play) {
        Log.i("reltime", "" + relTime + ",duration:" + mMovie.duration());
        this.invalidate();
    }
}

@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
    return true;
};

public boolean isPlay() {
    return play;
}

#1


53  

See this solution.

看到这个解决方案

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

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

It's an Android version of this post.

这是这篇文章的Android版本。

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

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.

要使用此类,下面是一个生成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();
}

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

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

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

#2


6  

This might help you. It's a class which is used to generate gif files.

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

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

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

#3


3  

If you only want to display these bitmaps like an animated gif, you can create an AnimatedDrawable using this code:

如果您只想像动画gif那样显示这些位图,可以使用以下代码创建AnimatedDrawable:

AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 10);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 50);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 30);
animation.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.imageView);
imageAnim.setImageDrawable(animation);

// start the animation!
animation.start();

#4


2  

It did very well for me, It create file fast and good quality images

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

//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!!!

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

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

#5


1  

Solution 1

解决方案1

Have you seen this article? ..... it's with source code..

你看过这篇文章吗? .....这是源代码..

As I know, I think android won't play gif animation image. You should use webview that will play gif animation..

据我所知,我认为android不会播放gif动画图片。您应该使用将播放gif动画的webview ..

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

and if you can..

如果你能......

http://androidosbeginning.blogspot.in/2010/09/gif-animation-in-android.html

http://androidosbeginning.blogspot.in/2010/09/gif-animation-in-android.html

Solution 2

解决方案2

Android provides Drawable Animation.

Android提供可绘制动画。

#6


0  

Gif is basically a set of images with a constant time delay in frames. You can not directly play gif in android. I've worked lot on playing gif animation in android.

Gif基本上是一组在帧中具有恒定时间延迟的图像。你无法在android中直接玩gif。我在android上玩gif动画很多。

  • When playing gif we also handle all calls of playing frames by our self. So this is not a best approach

    在玩gif时,我们也可以自己处理所有玩框架的调用。所以这不是最好的方法

  • If you have set of images just play them by a delay in frames. That would be the all you want.

    如果您有一组图像,只需按帧延迟播放即可。这就是你想要的一切。

  • You can not make gif in application by native code as well. There are some drawbacks as well for using gif format. Which I had faced in my game.

    您也不能通过本机代码在应用程序中生成gif。使用gif格式也有一些缺点。这是我在游戏中面临的问题。

  • If you have set of drawables you can use AnimationDrawable for showing aanimation like gif as well. It can also set the any View.

    如果你有一组drawable,你可以使用AnimationDrawable来显示像gif一样的动画。它还可以设置任何视图。

I had also made a custom view to play gif animations. First I load gif and convert it into InputStream then I pass it to my custom view class to play the gif.

我也制作了一个自定义视图来播放gif动画。首先我加载gif并将其转换为InputStream然后我将它传递给我的自定义视图类来播放gif。

 public class GifWebView extends View {
    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;
    private boolean play;

public GifWebView(Context context, InputStream stream) {
    super(context);
    mStream = stream;
    setPlay(true);
    mMovie = Movie.decodeStream(mStream);
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    final long now = SystemClock.uptimeMillis();

    if (mMoviestart == 0) {
        mMoviestart = now;
    }
    final int relTime = (int) ((now - mMoviestart) % mMovie.duration());

    mMovie.setTime(relTime);
    mMovie.draw(canvas, 20, 20);
    if (play) {
        Log.i("reltime", "" + relTime + ",duration:" + mMovie.duration());
        this.invalidate();
    }
}

@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
    return true;
};

public boolean isPlay() {
    return play;
}