Android加载gif图

 最近在项目中遇到需要在界面上显示一个本地的 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();  
        }  
    }  
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值