android -- 加载gif 防止oom

项目中涉及到gif图片的展示,原来使用的是gifview,但是当频繁的,加载过大图片的时候会出现OOM的问题,后来去github上面找相关的库:

https://github.com/koral--/android-gif-drawable

android-gif-drawable是比较好的,并能尽最大可能避免oom的库

下面讲下如何导入和使用该库:

这些事so文件,必须导入;还有就是引入jar包:

然后xml文件里面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loading_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/iv_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/dimen_48dp" />

    <TextView
        android:id="@+id/tv_iv_nonet_bg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_loading"
        android:layout_centerHorizontal="true"
        android:text="加载中.."
        android:textColor="@color/color_A3A3A3" />

</RelativeLayout>

注意红色部位就是对应加载gif图片的控件GifImageView

下面看下代码中如何使用:

GifImageView gifView = (GifImageView) findViewById(R.id.iv_loading);
        gifView.setImageResource((R.drawable.search_loading_bg));

第二行就是加载drawable的代码,至于其他用法,看该库github

原文地址:https://www.cnblogs.com/androidsuperman/p/4988084.html