1

我不会将简单的图像制作成像 GIF 这样的动画图像。我可以选择画廊的图像并存储在图像文件中。但是,我如何将图像文件修改为动画图像或 GIF 文件。

4

2 回答 2

3

您必须选择要制作 GIF 的图像,然后使用位图并在后台线程/异步任务中运行代码:

在这里,我使用了 Drawable,在您的情况下将图像转换为位图并使用它。

            Bitmap one=BitmapFactory.decodeResource(getResources(),R.drawable.neon0);
            Bitmap two=BitmapFactory.decodeResource(getResources(),R.drawable.neon1);
            Bitmap three=BitmapFactory.decodeResource(getResources(),R.drawable.neon2);


            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.start(bos);


            encoder.addFrame(one);
            encoder.addFrame(two);
            encoder.addFrame(three);
            encoder.finish();

            FileOutputStream outStream;
            try{
                outStream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + 
                "/IMAGES_GIF/" + "animated.gif");
                outStream.write(bos.toByteArray());
                outStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }

从以下位置获取课程:

https://gist.githubusercontent.com/wasabeef/8785346/raw/53a15d99062a382690275ef5666174139b32edb5/AnimatedGifEncoder.java

于 2017-05-03T11:05:38.310 回答
1

这个问题需要更多信息。该文件是否已经是 GIF 并且它没有动画,或者您是否正在尝试将图像转换为 GIF?后者是不可能的,一个 GIF 是一系列图像。

您必须在别处创建 GIF,最简单的方法是使用 Glide ( https://github.com/bumptech/glide ) 或 Picasso ( http://square.github.io/picasso/ ) 之类的库来加载 GIF。

于 2017-05-03T10:49:39.503 回答