Android显示Gif图片

来源:互联网 发布:dw软件使用教程 编辑:程序博客网 时间:2024/05/16 10:18

关于android显示gif图片的方法有许多种。我试了许多方法之后,觉得都不是很理想。庆幸的是,查看了这篇博客加载网络gif图片之后,我总算找到了理想的方法,在此博客的基础上,本博客将讲解得更详细,更具体。本博客讲解的《Android从网络上获取Gif图片并显示》所用的技术为:利用android开源库android-gif-drawable和android-async-http显示本地的gif图片和从网络上获取Gif图片并显示.即使是较大的gif图片,也不会报OOM异常。解决我们“Android从网络上获取gif图片并显示”的烦恼。对这两个开源库不是很了解的伙伴们,最好先了解,这样你会更容易看懂这篇博客哦

  1. 开源框架下载地址

    android-gif-drawable开源库

    android-async-http开源库

    下载有点麻烦的伙伴们,可以在下面下载我的demo,里面都集成了。
  2. 开源框架导入到项目中

    首先将android-gif-drawable开源库中Jni文件夹的文件和classes.jar包导入,然后将android-async-http开源库中的android-async-http-1.4.5.jar包导入。
    详细步骤请查阅:android-gif-drawable开源库导入到项目中

导入成功之后,项目目录如下:项目下lib下的目录

主要代码 片段

    public class MainActivity extends Activity {    private GifImageView gifImageView;    private GifDrawable gifDrawable;    private AsyncHttpClient asyncHttpClient;    private ProgressDialog dialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gifImageView = (GifImageView) findViewById(R.id.gif);        dialog = ProgressDialog.show(this, "加载网络gif图片", "加载中...");        getGifImage();    }    /**     * 获取网上gif图片     */    public void getGifImage() {        asyncHttpClient = new AsyncHttpClient();        //通过URL获取字节数组        asyncHttpClient                .get("http://img.juhe.cn/joke/201412/19/2EAFACE519BEAA9C6D139AAEE5CE1371.gif",                        new AsyncHttpResponseHandler() {                            @Override                            public void onSuccess(int i, Header[] headers, byte[] bytes) {                                try {                                    gifDrawable = new GifDrawable(bytes);                                } catch (IOException e) {                                    e.printStackTrace();                                }                                //给该gifImageView设置动画背景图                                gifImageView.setImageDrawable(gifDrawable);                                dialog.dismiss();                            }                            @Override                            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {                                    Toast.makeText(MainActivity.this, "获取网上gif图片失败", Toast.LENGTH_LONG).show();                                dialog.dismiss();                            }                        });    }}

上面的代码块主要讲述的是:通过asyncHttpClient 对象调用get方法返回网上gif图片的字节数组,再然后通过gifImageView对象调用setImageDrawable方法设置该视图gif图片。

布局文件代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="本地显示gif图片" />    <pl.droidsonroids.gif.GifImageView        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:src="@drawable/bird" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="网上获取gif图片并显示" />    <pl.droidsonroids.gif.GifImageView        android:id="@+id/gif"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>

本地gif图片:
这里写图片描述

运行之后动态图部分截图依次为如下


这里写图片描述


这里写图片描述


这里写图片描述

这样,Android从网络上获取Gif图片并显示就讲解完毕!是不是觉得用了开源框架之后,变得有点简单了!

本博客项目代码下载:

demo下载

如有错误或者疑问欢迎评论!

联系方式:840284038(QQ)

1 0
原创粉丝点击