1.项目引用Fresco库
在app目录下的build.gradle添加
dependencies {
...
//要添加的
implementation 'com.facebook.fresco:fresco:1.10.0'
}
如果是加载网络图片,需要获取网络请求权限
在AndroidManifest.xml中的manifest标签下添加
<uses-permission android:name="android.permission.INTERNET"/>
如果还要使用图片的本地缓存等,也需要申请对应的权限。
2.加载图片
2.1 简单使用SimpleDraweeView加载图片
在布局中加入SimpleDraweeView控件
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simple_drawee_view"
android:layout_width="200dp"
android:layout_height="200dp" />
在Java代码中开始加载图片
SimpleDraweeView mSimpleView = findViewById(R.id.simple_drawee_view);
mSimpleView.setImageURI(Uri.parse("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"));
这样可以了。当然SimpleDraweeView还有很多属性可以用。
Fresco官方详细介绍:https://www.fresco-cn.org/docs/using-drawees-xml.html
2.2 显示图片加载进度条
想要显示进度条只需要给SimpleDraweeView设置一个Hierarchy
GenericDraweeHierarchy hierarchy = GenericDraweeHierarchyBuilder
.newInstance(getResources())
.setProgressBarImage(new ProgressBarDrawable()).build();
mSimpleView.setHierarchy(hierarchy);
ProgressBarDrawable是Fresco提供的一个默认的进度条,我们也可以自定义进度条,继承Drawable并实现自己想要的效果
class CustomProgressBar extends Drawable {
@Override
protected boolean onLevelChange(int level) {
// level is on a scale of 0-10,000
// where 10,000 means fully downloaded
// your app's logic to change the drawable's
// appearance here based on progress
}
}
Fresco官方介绍:https://www.fresco-cn.org/docs/progress-bars.html
2.2 对显示的图片做出更多的控制
可以使用DraweeController,继承BaseControllerListener并选择实现自己需要的方法,在回调方法中实现自己的逻辑
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"))
.setOldController(mSimpleView.getController())
.setControllerListener(new ControllerListener())
.build();
mSimpleView.setController(controller);
private class ControllerListener extends BaseControllerListener<ImageInfo> {
public ControllerListener() {
super();
}
@Override
public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
}
@Override
public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
super.onIntermediateImageSet(id, imageInfo);
}
@Override
public void onFailure(String id, Throwable throwable) {
super.onFailure(id, throwable);
}
}
由于我也是初学Fresco,就不胡乱介绍了。更加详细的介绍请参考Fresco官方介绍:https://www.fresco-cn.org/docs/using-controllerbuilder.html
2.3 对图片的请求过程做处理
使用ImageRequest时可以设置监听事件,能够监听图片的所有请求过程,然后将ImageRequest使用DraweeController来加载图片
ImageRequest request = ImageRequestBuilder
.newBuilderWithSource(Uri.parse("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"))
.setResizeOptions(new ResizeOptions(500, 500))
.setRequestListener(mRequestListener)
.build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(mSimpleView.getController())
.setControllerListener(new ControllerListener())
.build();
mSimpleView.setController(controller);
private RequestListener mRequestListener = new RequestListener() {
@Override
public void onRequestStart(ImageRequest request, Object callerContext, String requestId, boolean isPrefetch) {
}
@Override
public void onRequestSuccess(ImageRequest request, String requestId, boolean isPrefetch) {
}
@Override
public void onRequestFailure(ImageRequest request, String requestId, Throwable throwable, boolean isPrefetch) {
}
@Override
public void onRequestCancellation(String requestId) {
}
@Override
public void onProducerStart(String requestId, String producerName) {
}
@Override
public void onProducerEvent(String requestId, String producerName, String eventName) {
}
@Override
public void onProducerFinishWithSuccess(String requestId, String producerName, Map<String, String> extraMap) {
}
@Override
public void onProducerFinishWithFailure(String requestId, String producerName, Throwable t, Map<String, String> extraMap) {
}
@Override
public void onProducerFinishWithCancellation(String requestId, String producerName, Map<String, String> extraMap) {
}
@Override
public void onUltimateProducerReached(String requestId, String producerName, boolean successful) {
}
@Override
public boolean requiresExtraMap(String requestId) {
return false;
}
};
.setResizeOptions(new ResizeOptions(500, 500))是设置加载图片的大小的。Fresco官方介绍地址:
https://www.fresco-cn.org/docs/image-requests.html
http://frescolib.org/docs/listening-to-events.html#motivation
3.总结
我只是写出了学习Fresco的一些笔记,Fresco功能很强大,比如还有自定义View,多图片处理,图片裁剪等等,想要学习请到Fresco官方网站学习,我的个人笔记仅供自己记录。
Fresco官方网站:https://www.fresco-cn.org/
网友评论