安卓中使用fresco加载Gif图片

来源:互联网 发布:倚天屠龙记星河源码 编辑:程序博客网 时间:2024/05/22 17:20

fresco是facebook推出的一款强大的android图片处理库,自从有了imageloader和fresco这样方便的第三方类库,图片OOM问题也就少了很多。

fresco github地址:https://github.com/facebook/fresco

用法其实非常简单

第一步,Studio中添加依赖
compile ‘com.facebook.fresco:fresco:0.8.1’

第二步,初始化,类似imageloader
Fresco.initialize(MainActivity.this);

第三步,布局里添加SimpleDraweeView组件

 <com.facebook.drawee.view.SimpleDraweeView            android:id="@+id/main_drawview"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="?attr/actionBarSize"            app:placeholderImage="@mipmap/ic_launcher" />

然后获取到该view设置Controller,uri为资源,AutoPlayAnimations为true则默认加载gif动画
java
SimpleDraweeView drawview = (SimpleDraweeView) findViewById(R.id.main_drawview);
DraweeController mDraweeController = Fresco.newDraweeControllerBuilder()
.setAutoPlayAnimations(true)
//加载drawable里的一张gif图
.setUri(Uri.parse("res://"+getPackageName()+"/"+R.drawable.panda))//设置uri
.build();
//设置Controller
drawview.setController(mDraweeController);

最终的效果如下,加了张功夫熊猫的图,项目中有些materialdesign的库,个人很喜欢materdesign的效果,之前写的,有兴趣的可以下下来看下,demo传到github上了,地址是https://github.com/shlockfan/MyMaterial

效果图

头一次写博客,没写多少东西,大家相互学习

0 1
原创粉丝点击