引入Fresco框架

Fresco 中设计有一个叫做 Image Pipeline 的模块。它负责从网络,从本地文件系统,本地资源加载图片。为了最大限度节省空间和CPU时间,它含有3级缓存设计(2级内存,1级磁盘)。

在这里简单的介绍一下使方法:更详细的使用方法以及API请参照https://www.fresco-cn.org/ 中午网站

1、添加相应的依赖

在app下的 bulid.gradle 下添加相应的依赖

// 其他依赖
compile 'com.facebook.fresco:fresco:0.12.0'
// 在 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'

2、从网络获取图片的同时不要忘记添加网络访问的权限

 <uses-permission android:name="android.permission.INTERNET"/>

3、在APP 的入口处,对 fresco进行初始化

Fresco.initialize(this);

Fresco.initialize(this);要放在

setContentView(R.layout.activity_main);之前做初始化

4、有点需要特别的注意,使用Fresco的时候,必须使用它自带的 图片装载布局

  <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/my_image_view"
        android:layout_height="300dp"
        android:layout_width="300dp"
        fresco:placeholderImage="@mipmap/ic_launcher"
        />

而非ImageView

5、加载一张普通的从网络获取的图片

 SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);

        draweeView.setImageURI(uri);


6、加载一张从网络获取的GIF图片

SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
  DraweeController mDraweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)
                //设置uri,加载本网络gif资源
                .setUri(Uri.parse("http://img4.178.com/acg1/201506/227753817857/227754566617.gif"))//设置uri
                .build();
//设置Controller
        draweeView.setController(mDraweeController);

7、设置占位图

因为图片在加载出来之前,会有一段时间是空白的,为了看上去美观一些,我们需要加载一张过渡的图片

fresco:placeholderImage="@mipmap/ic_launcher"

但是这里有一点需要注意:

<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">

红色部位代码必须加上

需要了解更多,请直接访问https://www.fresco-cn.org/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值