引入Fresco
这里告诉你如何在项目中引入 Fresco.
使用 Android Studio 或者其他 Gradle 构建的项目
编辑 build.gradle
文件:
1 2 3 4 | dependencies { // 其他依赖 compile 'com.facebook.fresco:fresco:0.12.0' } |
下面的依赖需要根据需求添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | dependencies { // 在 API < 14 上的机器支持 WebP 时,需要添加 compile 'com.facebook.fresco:animated-base-support:0.12.0' // 支持 GIF 动图,需要添加 compile 'com.facebook.fresco:animated-gif:0.12.0' // 支持 WebP (静态图+动图),需要添加 compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0' // 仅支持 WebP 静态图,需要添加 compile 'com.facebook.fresco:webpsupport:0.12.0' } |
Eclipse ADT
下载 zip 文件.
解压后,你会看到一个目录:frescolib,注意这个目录。
- 从菜单 “文件(File)”,选择导入(Import)
- 展开 Android, 选择 “Existing Android Code into Workspace”, 下一步。
- 浏览,选中刚才解压的的文件中的 frescolib 目录。
- 这5个项目应该被添加到工程:
drawee
,fbcore
,fresco
,imagepipeline
,imagepipeline-base
。请确认这5个项目一定是被选中的。点击完成。其他的项目参考之前 Gradle的额外依赖介绍。 - 右键,项目,选择属性,然后选择 Android。
- 点击右下角的 Add 按钮,选择 fresco,点击 OK,再点击 OK。
现在,fresco 就导入到项目中了,你可以开始编译了。如果编译不通过,可以尝试清理资源,或者重启 Eclipse。
如果你想在网络层使用 OkHttp,请看这里.
如果 support-v4 包重复了,删掉 frescolib/imagepipeline/libs 下的即可。
开始使用 Fresco
如果你仅仅是想简单下载一张网络图片,在下载完成之前,显示一张占位图,那么简单使用 SimpleDraweeView 即可。
在加载图片之前,你必须初始化Fresco
类。你只需要调用Fresco.initialize
一次即可完成初始化,在 Application
里面做这件事再适合不过了(如下面的代码),注意多次的调用初始化是无意义的。
1 2 3 4 5 6 7 8 | [MyApplication.java] public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } } |
做完上面的工作后,你需要在 AndroidManifest.xml
中指定你的 Application 类。为了下载网络图片,请确认你声明了网络请求的权限。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <manifest ... > <uses-permission android:name="android.permission.INTERNET" /> <application ... android:label="@string/app_name" android:name=".MyApplication" > ... </application> ... </manifest> |
在xml布局文件中, 加入命名空间:
1 2 3 4 5 6 | <!-- 其他元素--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent"> |
加入SimpleDraweeView
:
1 2 3 4 5 6 | <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="130dp" android:layout_height="130dp" fresco:placeholderImage="@drawable/my_drawable" /> |
开始加载图片:
1 2 3 | Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png"); SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view); draweeView.setImageURI(uri); |
剩下的,Fresco会替你完成:
- 显示占位图直到加载完成;
- 下载图片;
- 缓存图片;
- 图片不再显示时,从内存中移除;
等等等等。
支持的URI
Fresco 支持许多URI格式。
特别注意:Fresco 不支持 相对路径的URI. 所有的 URI 都必须是绝对路径,并且带上该 URI 的 scheme。
如下:
类型 | SCHEME | 示例 |
---|---|---|
远程图片 | http://, https:// | HttpURLConnection 或者参考 使用其他网络加载方案 |
本地文件 | file:// | FileInputStream |
Content provider | content:// | ContentResolver |
asset目录下的资源 | asset:// | AssetManager |
res目录下的资源 | res:// | Resources.openRawResource |
Uri中指定图片数据 | data:mime/type;base64, | 数据类型必须符合 rfc2397规定 (仅支持 UTF-8) |
res 示例:
1 | Uri uri = Uri.parse("res://包名(实际可以是任何字符串甚至留空)/" + R.drawable.ic_launcher); |
注意,只有图片资源才能使用在Image pipeline中,比如(PNG)。其他资源类型,比如字符串,或者XML Drawable在Image pipeline中没有意义。所以加载的资源不支持这些类型。
像ShapeDrawable
这样声明在XML中的drawable可能引起困惑。注意到这毕竟不是图片。如果想把这样的drawable作为图像显示,那么把这个drawable设置为占位图,然后把URI设置为null
。
中文文档地址:https://www.fresco-cn.org/docs/getting-started.html