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

我正在寻找在本机Android应用程序中创建动画GIF的简单方法。源文件应为JPEG(来自相机或其他设备),输出应另存为GIF在设备上。

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

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

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

6个解决方案

56 votes

请参阅此解决方案。

[HTTPS://GitHub.com/NBA大佬/Android-gif-encoder]

这是此文章的Android版本。

[HTTP://呜呜呜.将app IT.com/blog/2008/12/04/就2么-animated-gif-encoder/]

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

public byte[] generateGIF() {
ArrayList 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();
}

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

FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/generate_gif/test.gif");
outStream.write(generateGIF());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
lifelogger answered 2020-07-24T06:06:33Z
6 votes

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

[HTTP://Elliot.客人哦哦.net/software/Java/gif sequence writer/]

Jatin Malwal answered 2020-07-24T06:06:57Z

4 votes

它对我来说做得很好,可以快速创建文件并提供高质量的图像

//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]
Đăng Huân answered 2020-07-24T06:07:22Z

3 votes

如果只想将这些位图显示为gif动画,则可以使用以下代码创建Animated Drawable:

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();
Abeer Iqbal answered 2020-07-24T06:07:42Z

1 votes

解决方案1

您看过这篇文章吗? .....附带源代码..

据我所知,我认为android不会播放gif动画图像。 您应该使用将播放gif动画的网络视图。

[HTTP://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gif是-in-Android-part-1/]

如果可以的话..

[HTTP://Android OS beginning.blogspot.in/2010/09/gif-animation-in-Android.HTML]

解决方案2

Android提供了Drawable Animation。

Bhavesh Hirpara answered 2020-07-24T06:08:32Z

0 votes

Gif基本上是一组具有恒定时间延迟(以帧为单位)的图像。 您不能直接在android中播放gif。 我在Android上播放gif动画方面做了大量工作。

播放gif时,我们还会处理所有播放帧的调用自。 所以这不是最好的方法

如果您有一组图像,则延迟播放它们。 那就是你想要的。

您也不能通过本机代码在应用程序中创建gif。 有使用gif格式也有一些缺点。 我在游戏中遇到的问题。

如果您有一组可绘制对象,则可以使用AnimationDrawable来显示动画,例如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;
}
Jatin Malwal answered 2020-07-24T06:09:15Z