官方网站:https://fresco-cn.org/
为什么使用Fresco:
Fresco 会替我们完成,显示占位图直到加载完成,下载图片,缓存图片,图片不再显示时候,从内存中移出;
注意Fresco的图片的宽高一定不能都是wrap_content,如果都是的话就不会显示图片了
0: 首先需要导入Frsco的包
1: 然后需要添加网络的访问权限
++++++++++++++++++++++++++++++++++++
2: 然后新建一个Application 在这个里面执行Fresco.initialize(context)的初始化
3:然后在xml 的布局文件中,加入命名空间
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:fresco="https://schemas.android.com/apk/res-auto"
4:在布局中加入SimpleDraweeView,注意这里的placeholderImage 是用来显示站位图的。
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:placeholderImage="@drawable/my_drawable"
/>
5: setImageURI()的方法来添加图片。
剩下的Fresco 会替我们完成,显示占位图知道加载完成,下载图片,缓存图片,图片不再显示时候,从内存中移出。
6:其中setImageURI(),
如果是网络上面的图片直接就是Uri.parse(网址),
如果是包里面的文件就是使用res://mipmap/+R.mipmap.yoyo
如果是android 手机目录中的文件就要使用file:///data/data/com.example.xue.frescodemo/files/345.jpg 注意 file是三条杠
如果需要用到mipmap里面的图片使用:
Uri uri = Uri.parse("res://包名(实际可以是任何字符串甚至留空)/" + R.drawable.ic_launcher)
draweeView_url.setImageURI(Uri.parse("https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0Bx4BSt6jniD7NndTQW9VZTlZV2s/materialdesign_principles_bold.png"));
draweeView_res.setImageURI(Uri.parse("res://mipmap/"+R.mipmap.yoyo));
draweeView_local.setImageURI(Uri.parse("file:///data/data/com.example.xue.frescodemo/files/345.jpg"));
7:还可以设置进度条:使用的方法是setProgressBarImage
++++++++++++++++++++++++++++++++++++++++++++++为什么使用Fresco