Fresco 是 facebook 的开源类库,它支持更有效的加载网络图片以及资源图片。它自带三级缓存功能,让图片显示更高效。
介绍android
Fresco 是一个强大的图片加载组件。 Fresco 中设计有一个叫作 image pipeline 的模块。它负责从网络,从本地文件系统,本地资源加载图片。为了最大限度节省空间和CPU时间,它含有3级缓存设计(2级内存,1级文件)。 Fresco 中设计有一个叫作 Drawees 模块,方便地显示loading图,当图片再也不显示在屏幕上时,及时地释放内存和空间占用。 Fresco 支持 Android2.3(API level 9) 及其以上系统。
简单来讲,只须要三步。git
1. 添加依赖 2. 初始化Fresco 3. 编写布局 4. 指定Uri
在你的 模块级别 的gradle中写下:github
compile 'com.facebook.fresco:fresco:0.10.0'
在你的自定义application中,或者在Activity.onCreate方法内,setContentView以前调用:缓存
Fresco.initialize(this);
声明命名空间 xmlns:fresco="http://schemas.android.com/apk/res-auto"网络
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto">
写一个SimpleDraweeViewapp
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view0" android:layout_width="200dp" android:layout_height="200dp" />
final String str2 = "http://h.hiphotos.baidu.com/zhidao/pic/item/279759ee3d6d55fb65c51e786c224f4a20a4dd69.jpg"; Uri uri = Uri.parse(str2); my_image_view0.setImageURI(uriERR);
继承于 View, 负责图片的显示。通常状况下,使用SimpleDraweeView 便可
ImageRequest存储着Image Pipeline处理被请求图片所须要的有用信息(Uri、是否渐进式图片、是否返回缩略图、缩放、是否自动旋转等)。
你必须声明 android:layout_width 和 android:layout_height。 若是没有在XML中声明这两个属性,将没法正确加载图像。
Drawees 不支持 wrap_content 属性。ide
这么作是有理由的:布局
所下载的图像可能和占位图尺寸不一致,若是设置出错图或者重试图的话,这些图的尺寸也可能和所下载的图尺寸不一致。 若是大小不一致,假设使用的是 wrap_content,图像下载完以后,View将会从新layout,改变大小和位置。这将会致使界面跳跃。
考虑到缓存的图片会根据你的尺寸进行缩略图,手机的屏幕会在旋转而致使imageview大小改变等,这些都会致使图片没法正常显示。gradle
只有但愿显示固定的宽高比时,可使用wrap_content。
若是但愿图片以特定的宽高比例显示,例如 4:3,能够在XML中指定:动画
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="20dp" android:layout_height="wrap_content" fresco:viewAspectRatio="1.33" <!-- other attributes -->
也能够在代码中指定显示比例:
mSimpleDraweeView.setAspectRatio(1.33f);
使用 progressBarImage 指定 加载时显示的图片
使用 failureImage 指定 加载失败的显示的图片
使用 placeholderImage 指定占位图
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view0" android:layout_width="200dp" android:layout_height="200dp" fresco:progressBarImage="@drawable/loading" fresco:progressBarImageScaleType="centerInside" fresco:failureImage="@drawable/error2" fresco:failureImageScaleType="centerInside" fresco:actualImageScaleType="centerCrop" fresco:placeholderImage="@drawable/loading" />
在加载失败时,能够设置点击从新加载。这时提供一个图片,加载失败时,会显示这个图片(而不是失败提示图片),提示用户点击重试。
在ControllerBuilder 中以下设置:
.setTapToRetryEnabled(true)
在xml中指定加载失败后提示重试的图片
fresco:retryImage="@drawable/retrying" fresco:retryImageScaleType="centerCrop"
并在ControllerBuilder 中以下设置:
.setTapToRetryEnabled(true)
加载失败时,image pipeline 会重试四次;若是仍是加载失败,则显示加载失败提示图片。
实现一个圆角图片是这么的容易,仅仅在xml布局里声明开启圆角,并指定 radius 便可。支持对四个角任意组合的圆角。
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_viewRound" android:layout_width="200dp" android:layout_height="200dp" fresco:roundedCornerRadius="30dp" fresco:roundTopLeft="true" fresco:roundTopRight="false" fresco:roundBottomLeft="false" fresco:roundBottomRight="true" fresco:placeholderImage="@drawable/loading" />
Fresco 支持渐进式的网络JPEG图。在开始加载以后,图会从模糊到清晰渐渐呈现。
你能够设置一个清晰度标准,在未达到这个清晰度以前,会一直显示占位图。
渐进式JPEG图仅仅支持网络图
得到SimpleDraweeView
my_image_view0 = (SimpleDraweeView) findViewById(R.id.my_image_view0);
构建ImageRequest加载图片
/** * 演示:逐渐加载的图片,即,从模糊逐渐清晰。须要图片自己也支持这种方式 */ private void showProgressiveJPEGs() { final String str3_progressive = "http://pooyak.com/p/progjpeg/jpegload.cgi?o=1"; Uri uri = Uri.parse(str3_progressive); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setProgressiveRenderingEnabled(true) .build(); DraweeController controller = Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setOldController(my_image_view0.getController()) .build(); my_image_view0.setController(controller); }
Fresco 支持 GIF 和 WebP 格式的动画图片。对于 WebP 格式的动画图的支持包括扩展的 WebP 格式,即便 Android 2.3及其之后那些没有原生 WebP 支持的系统。
若是你但愿图片下载完以后自动播放,同时,当View从屏幕移除时,中止播放,只须要在 image request 中简单设置,以下:
Uri uri; DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); mSimpleDraweeView.setController(controller);
有时候咱们须要监听图片显示的过程,好比在失败,中间过程,成功时作一些事情。咱们能够这么作:
在ControllerListener 的回调方法里处理 失败,中间过程,成功时的事情
Uri uri;
DraweeController controller = Fresco.newControllerBuilder()
.setControllerListener(controllerListener)
.setUri(uri);
.build();
mSimpleDraweeView.setController(controller);
上面的代码指定了一个 ControllerListener ,它包含一些回调方法:
onFinalImageSet 加载完成 onIntermediateImageSet 加载中间过程 onFailure 加载失败
Fresco 提供了一个 推荐继承BaseControllerListener ,继承自 推荐继承BaseControllerListener 更方便,示例代码:
ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet( String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) { if (imageInfo == null) { return; } QualityInfo qualityInfo = imageInfo.getQualityInfo(); FLog.d("Final image received! " + "Size %d x %d", "Quality level %d, good enough: %s, full quality: %s", imageInfo.getWidth(), imageInfo.getHeight(), qualityInfo.getQuality(), qualityInfo.isOfGoodEnoughQuality(), qualityInfo.isOfFullQuality()); } @Override public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) { FLog.d("Intermediate image received"); } @Override public void onFailure(String id, Throwable throwable) { FLog.e(getClass(), throwable, "Error loading %s", id) } };
Fresco使用 Uri 对象指定要显示的图片
res 示例:
Uri uri = Uri.parse("res://包名(实际能够是任何字符串甚至留空)/" + R.drawable.ic
Fresco 支持许多URI格式。见下表:
类型 Scheme 示例 远程图片: http://, https:// HttpURLConnection 或者参考 使用其余网络加载方案 本地文件: file:// FileInputStream Content provider: content:// ContentResolver asset目录下的资源: asset:// AssetManager res目录下的资源: res:// Resources.openRawResource
特别注意:Fresco 不支持 相对路径的URI. 全部的URI都必须是绝对路径,而且带上该URI的scheme。