How to create an animated GIF from JPEGs in Androi

2019-01-07 04:04发布

问题:

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.

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

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.

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

回答1:

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();
        }


回答2:

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

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



回答3:

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

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:

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



回答5:

Solution 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..

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

Solution 2

Android provides Drawable Animation.



回答6:

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.

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

  • 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.

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

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.

 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;
}