第一步: 导依赖
//fresco基础
implementation 'com.facebook.fresco:fresco:1.11.0'
//下面的依赖根据需求,用到哪个模块,就导入对应的依赖即可.
// 仅支持 WebP 静态图,需要添加
compile 'com.facebook.fresco:webpsupport:0.14.1'
// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.14.1'
// 支持 WebP 静态图及 WebP 动图,需要添加
compile 'com.facebook.fresco:animated-webp:0.14.1'
compile 'com.facebook.fresco:webpsupport:0.14.1'
// 在 API < 14 上的机器支持 WebP 时,需要添加
compile 'com.facebook.fresco:animated-base-support:0.14.1'
第二步:布局
XML: xmlns:fresco="http://schemas.android.com/apk/res-auto"
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/SDV"
android:layout_width="254dp"
android:layout_height="254dp"
fresco:placeholderImage="@mipmap/ic_launcher"
/>
第三步:全局设置
//设置磁盘缓存(可以不要)
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this)
//设置缓存的目录名字
.setBaseDirectoryName("images")
//设置缓存的路径
.setBaseDirectoryPath(Environment.getExternalStorageDirectory())
.build();
//设置磁盘缓存的配置,生成配置未见
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
.setMainDiskCacheConfig(diskCacheConfig)
.build();
//必须!!!!
Fresco.initialize(this,config);
第四步:代码中添加图片网址
//加载网络图片的网址
Uri uri = Uri.parse("图片网址");
//初始化控件
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
//加载图片
draweeView.setImageURI(uri);
第五步:如果需要联网,添加网络权限