引入Fresco框架

来源:互联网 发布:windows直接进入桌面 编辑:程序博客网 时间:2024/06/03 06:05

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/

原创粉丝点击