Android加载gif图

来源:互联网 发布:淘宝旗舰店有假货吗 编辑:程序博客网 时间:2024/05/16 18:27
 最近在项目中遇到需要在界面上显示一个本地的 GIF 图。按照惯例我直接用了 Glide 框架来实现。
Glide 地址: https://github.com/bumptech/glide具体的实现代码如下:Glide.with( this ).asGif().load( R.drawable.yiba_location ).into( location_gif1 ) ;
运行的效果很卡顿,我怀疑是不是方法没有用对,调了压缩模式,还是卡顿;调了缓存模式,还是卡顿。看了一下我的 gif 图,大小还是 800K ,是不是图片太大了,换了一张 100K 的 gif 图,这次显示的效果很好,gif 图播放的很流畅。至此,得出结论:Glide 框架自身的原因,播放大尺寸的 Gif 图的效果不是很理想。
github排名第一的 android-gif-drawable 库 start 有 4.8K , 这个应该不错,试试吧。
android-gif-drawable : https://github.com/koral–/android-gif-drawable
引用:
 compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
直接把布局文件中的 ImageView 替换为 GifImageView
写布局文件activity_main.xml:
<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:background="@android:color/white"  
    android:orientation="vertical" >  
    <pl.droidsonroids.gif.GifImageView  
        android:id="@+id/location_gif1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
    <pl.droidsonroids.gif.GifImageView  
        android:id="@+id/location_gif2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
</LinearLayout>  
public class MainActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        GifImageView gifImageView1 = (GifImageView) findViewById(R.id.location_gif1);  
        GifImageView gifImageView2 = (GifImageView) findViewById(R.id.location_gif2);  
        try {  
            // 如果加载的是gif动图,第一步需要先将gif动图资源转化为GifDrawable  
            // 将gif图资源转化为GifDrawable  
            GifDrawable gifDrawable = new GifDrawable(getResources(), R.drawable.loading);  
            // gif1加载一个动态图gif  
            gifImageView1.setImageDrawable(gifDrawable);  
            // 如果是普通的图片资源,就像Android的ImageView set图片资源一样简单设置进去即可。  
            // gif2加载一个普通的图片(如png,bmp,jpeg等等)  
            gifImageView2.setImageResource(R.drawable.ic_launcher);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}
原创粉丝点击