如何在Android中使用JPEG创buildGIFanimation(开发)

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

我不想知道如何播放animation或GIFanimation文件。

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

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

看到这个解决scheme

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

这是这篇文章的Android版本。

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

要使用这个类,下面是一个示例帮助方法来生成GIF字节数组。 请注意,getBitmapArray()函数是一种一次性返回映像适配器中的所有位图文件的方法。 因此,input是一个适配器中的所有位图文件,输出是一个可以写入文件的字节数组。

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

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

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

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

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

如果你只想显示这些位图像GIFanimation,你可以用下面的代码创build一个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(); 

它为我做得很好,它创build文件快速和高质量的图像

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

解决scheme1

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

据我所知,我认为Android不会播放GIFanimation图像。 你应该使用webview,将播放gifanimation..

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

如果你可以

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

解决scheme2

Android提供可绘制animation 。

Gif基本上是一组具有固定时间延迟的图像。 你不能在android中直接玩gif。 我在android中玩gifanimation很多。

  • 在玩GIF的时候,我们也会自己处理所有的播放帧。 所以这不是一个最好的方法

  • 如果你有一组图像只是在帧中延迟播放它们。 那将是你想要的。

  • 本地代码也无法在应用程序中生成gif。 使用gif格式也有一些缺点。 我在我的游戏中遇到过的

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

我也做了一个自定义的视图来玩gifanimation。 首先我加载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; }