1. 简单使用
2. 属性控制
3. 播放动态webp
4. 监听webp播放及控制webp播放次数
5. 预取图片
6. 连续加载多张动图闪帧解决方案

1. 简单使用

1)添加依赖:

dependencies {//必须添加implementation 'com.facebook.fresco:fresco:1.9.0'//依需要添加// For animated GIF supportimplementation 'com.facebook.fresco:animated-gif:1.9.0'// For WebP support, including animated WebPimplementation 'com.facebook.fresco:animated-webp:1.9.0'implementation 'com.facebook.fresco:webpsupport:1.9.0'// For WebP support, without animationsimplementation 'com.facebook.fresco:webpsupport:1.9.0'// Provide the Android support library (you might already have this or a similar dependency)implementation 'com.android.support:support-core-utils:24.2.1'
}

2)Application中初始化
在你AndroidManifest中指定的Application的onCreate方法中初始化,如果没有指定,需要继承并指定。

public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();Fresco.initialize(this);}
}
 <manifest... ><application...android:label="@string/app_name"android:name=".MyApplication">...</application>...</manifest>

3)编写xml,记得指定fresco命名空间,需要注意的是SimpleDraweeView必须指定确定大小,不支持wrap_content

xmlns:fresco="http://schemas.android.com/apk/res-auto"
<com.facebook.drawee.view.SimpleDraweeViewandroid:id="@+id/my_image_view"android:layout_width="130dp"android:layout_height="130dp"fresco:placeholderImage="@drawable/my_drawable" />

4)指定Uri

Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/master/docs/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

或直接在xml中指定
fresco:actualImageUri
fresco:actualImageResource

Fresco 不支持 相对路径的URI. 所有的 URI 都必须是绝对路径,并且带上该 URI 的 scheme。

2. 属性控制

属性都基本可以在xml和代码中控制,见文生义,这里就不详细解释了

<com.facebook.drawee.view.SimpleDraweeViewandroid:id="@+id/my_image_view"android:layout_width="20dp"android:layout_height="20dp"fresco:fadeDuration="300"fresco:actualImageScaleType="focusCrop"fresco:placeholderImage="@color/wait_color"fresco:placeholderImageScaleType="fitCenter"fresco:failureImage="@drawable/error"fresco:failureImageScaleType="centerInside"fresco:retryImage="@drawable/retrying"fresco:retryImageScaleType="centerCrop"fresco:progressBarImage="@drawable/progress_bar"fresco:progressBarImageScaleType="centerInside"fresco:progressBarAutoRotateInterval="1000"fresco:backgroundImage="@color/blue"fresco:overlayImage="@drawable/watermark"fresco:pressedStateOverlayImage="@color/red"fresco:roundAsCircle="false"fresco:roundedCornerRadius="1dp"fresco:roundTopLeft="true"fresco:roundTopRight="false"fresco:roundBottomLeft="false"fresco:roundBottomRight="true"fresco:roundWithOverlayColor="@color/corner_color"fresco:roundingBorderWidth="2dp"fresco:roundingBorderColor="@color/border_color"
/>

3. 播放动态webp

1)自动播放:

Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setAutoPlayAnimations(true). // 其他设置.build();
mSimpleDraweeView.setController(controller);

2)手动控制播放播放

ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {@Overridepublic void onFinalImageSet(String id,@Nullable ImageInfo imageInfo,@Nullable Animatable anim) {if (anim != null) {// 其他控制逻辑,可以在此处设置图片加载结束的标志位anim.start();}}
};Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setControllerListener(controllerListener)// 其他设置.build();
mSimpleDraweeView.setController(controller);

使用animatable.start()开始播放动画,animatable.stop()停止播放动画

4. 控制动态webp播放次数,监听webp播放,获取动图时长

1)控制播放次数
需添加依赖:
implementation 'com.facebook.fresco:animated-drawable:1.9.0'

final DraweeController controller = Fresco.newDraweeControllerBuilder().setAutoPlayAnimations(true).setUri(uri).setControllerListener(new BaseControllerListener<ImageInfo>() {@Overridepublic void onFinalImageSet(String id,@Nullable ImageInfo imageInfo,@Nullable Animatable animatable) {if (animatable instanceof AnimatedDrawable2) {AnimatedDrawable2 animatedDrawable = (AnimatedDrawable2) animatable;animatedDrawable.setAnimationBackend(new LoopCountModifyingBackend(animatedDrawable.getAnimationBackend(), 3));//设置循环次数}}}).build();
simpleDraweeView.setController(controller);
public class LoopCountModifyingBackend extends AnimationBackendDelegate {private int mLoopCount;public LoopCountModifyingBackend(@Nullable AnimationBackend animationBackend,int loopCount) {super(animationBackend);mLoopCount = loopCount;}@Overridepublic int getLoopCount() {return mLoopCount;}
}

2)监听动态webp

animatedDrawable.setAnimationListener()

animaetdDrawable是AnimatedDrawable2的实例

3)获取动图时长

animatedDrawable.getLoopDurationMs()

其它的还有:

animatedDrawable.getFrameCount()//获取帧数
animatedDrawable.getLoopCount()//获取循环次数

5. 预取图片/预加载图片

获取ImagePipeline:

Fresco.getImagePipeline()

获取ImageRequest

ImageRequest.fromFile(file);
ImageRequest.fromUri(uri);

预加载到磁盘缓存:

imagePipeline.prefetchToDiskCache(imageRequest, callerContext);

预加载到内存缓存:

imagePipeline.prefetchToBitmapCache(imageRequest, callerContext);

取消预加载:

DataSource<Void> prefetchDataSource = imagePipeline.prefetchTo...;
prefetchDataSource.close();

6. 连续播放多张动图闪帧解决方案

连续播放多张动图,中间会有几十毫秒的加载间隔。
本来想将动态webp缓存到内存,后来发现行不通,动态的webp貌似无法缓存到内存。
最后另辟蹊径,才用setPlaceHolder(),使其先显示的是动图的第一帧,解决问题。
代码如下:

 GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
/*设置placeholder,减少连续加载两个动图时的间隙,同时设置渐变时间为0,动态webp不会缓存到内存*/
GenericDraweeHierarchy hierarchy = builder.setPlaceholderImage(ResourcesCompat.getDrawable(getResources(), drawableId, null)).setFadeDuration(0).build();DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setControllerListener(controllerListener).build();
mSimpleDraweeView.setHierarchy(hierarchy);
mSimpleDraweeView.setController(controller);

需要注意的是,设置placeholder后,在播放下一张动图时,如果下一张动图没有设置placeholder,那么上一张的placeholder会闪一下,所以在加载下一张动图前需要设置placeholder或者将placeholder置为空。

refer:
http://frescolib.org/
https://www.fresco-cn.org/
https://github.com/facebook/fresco

Fresco加载图片使用笔记--基本使用,播放动态wbep,控制播放次数,预加载,闪帧解决相关推荐

  1. 超详细的图片预加载和懒加载教程

    最近接手一个项目 . 结果光安装依赖都出现了一堆 麻烦 . 好不容易处理完一个 , 又来一个 .头疼啊 看到之前有一些预加载的学习笔记.于是又查查找找 ,想想写写 把预加载和懒加载的笔记写完整 发现制 ...

  2. android调用h5预加载图片,使用HTML5的页面资源预加载(Link prefetch)功能加速你的页面加载速度...

    不管是浏览器的开发者还是普通web应用的开发者,他们都在做一个共同的努力:让Web浏览有更快的速度感觉.有很多已知的技术都可以让你的网站速度变得更快:使用CSS sprites,使用图片优化工具,使用 ...

  3. Opencv判断是否加载图片的两种方法

    加载图片是图像处理最基本的操作,但有的时候我们加载图片会因为图片路径不正确 或者没有图片等原因而报错,经常写代码的凭经验就知道是哪错了,但初学者一般是看不懂这 些错误,就会很恼火了,如果加个判断语句就 ...

  4. 前端项目分析:我是如何做图片优化的(预加载、懒加载和延迟加载)

    众所周知:前端页面上的图片是优化时最重要也是最令人头疼的部分,花费了几个月的时间才优化到令自己满意的一半程度,,,唉,一言难尽啊! 在此将几种方法总结一下,希望能帮到不少人吧- 图片的优化有两种方式: ...

  5. iOS开发 关于tableView加载图片时出现卡顿时的解决办法

    新手做开发的时候一般都会遇到使用tableView从网上加载图片并显示图文的时候会有卡顿现象,而这种卡顿现象也是因为多种原因造成的.一般可以分为两种:一种是由于网上加载图片需要动态定义cell的高度( ...

  6. Android加载图片内存溢出问题解决方法

    这篇文章主要介绍了Android加载图片内存溢出问题解决方法,本文讲解使用BitmapFactory.Options解决内存溢出问题,需要的朋友可以参考下 1. 在Android软件开发过程中,图片处 ...

  7. jQuery插件之图片预加载

    背景: 图片是web页面的重要组成部分,也是前端页面优化的重要内容.当用户访问一个比较庞大的页面时,若相关资源没有提前加载,可能会展示给用户一片空白,从而导致用户流失等:再比如受网速的影响,资源加载时 ...

  8. canvas学习day3——加载图片loadImage函数,理解回调函数

    回调函数 回调函数理解 你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里,你的电话号码就 ...

  9. android异步加载图片并缓存到内存和sd卡上,Android批量图片加载经典系列——采用二级缓存、异步加载网络图片...

    http://www.cnblogs.com/jerehedu/p/4560119.html 2015-06-08 09:20 by 杰瑞教育, 232 阅读, 1 评论, 收藏, 编辑一.问题描述 ...

最新文章

  1. BZOJ 1821 [JSOI2010] Group 部落划分 Group
  2. [转] Envelop
  3. 【特征工程】与【表示学习】
  4. oracle 11g-R2安装
  5. System.currentTimeMillis()与日期之间的相互转换
  6. Spring请求参数测试
  7. Visual Basic.Net连各种数据库的几种方法
  8. POP3协议(电子邮件邮局协议)中UIDL和TOP命令在实际使用中的作用
  9. 新题速看~2021阿里、腾讯、字节都在问的SQL数据库笔试题及答案都给你整理好啦
  10. 题解 P1340 【兽径管理】
  11. 深入浅出程序设计竞赛(基础篇)
  12. 职场纵横:IT职位全面解析(计算机类要找工作的朋友多看看)
  13. ubuntu美化--壁纸软件
  14. python画人物代码_代码绘制一只小猪佩奇---python篇
  15. 为什么 Flutter 是跨平台开发的终极之选
  16. 【PPPAR】PPPAR入门知识点
  17. HP路由器和交换机日志分析
  18. 【Microsoft Azure 的1024种玩法】二十六. 在Azure VM中手动部署Windows Admin Center管理平台
  19. 进程间通信的六大方式
  20. Luogu P3853 路标设置

热门文章

  1. Linux下简单socket编程
  2. PropertyGrid使用方法
  3. gt9xx电容屏驱动分析
  4. 猿辅导转行卖羽绒服?网友:这是段子吗?会有新某方棉鞋、学某思秋裤吗?...
  5. [bzoj3597][SCOI2014]方伯伯运椰子
  6. 雪在第一天的冒险 - 2014 在冬天的第一场雪立场坚定?
  7. Introduction to RTMFP
  8. Java设计模式——装饰模式(装饰设计模式)详解
  9. 我的世界国际版仿java版材质包_我的世界国际版如何更换材质包和光影
  10. 爬取携程中评论的数据