强大的 Fresco(加载网络和本地图片)简单使用

来源:互联网 发布:二本学校当老师 知乎 编辑:程序博客网 时间:2024/06/13 21:51

目录(?)[+]

  1. Fresco 是一个强大的图片加载组件

    Fresco 是一个强大的图片加载组件。

    Fresco 是facebook推出的一款强大的android图片处理库,能够从网络,从本地文件系统,本地资源加载图片。为了最大限度节省空间和CPU时间,它含有3级缓存设计(2级内存,1级文件)。官方配置文档中文版 :http://fresco-cn.org/docs/index.html 

    直接上代码 –简单使用

    1、导包,build.gradle里:    
    compile 'com.facebook.fresco:fresco:0.11.0'
    2、在Aplication里提前实例化下:
    public class MyApplication extends Application {    public static boolean b;    @Override    public void onCreate() {        initImageLoader(getApplicationContext());        Fresco.initialize(getApplicationContext());        super.onCreate();    }
    }

    注意:记得在Androidmanifest.xml清单文件中声明Aplication
    <application    android:name=".MyApplication"
    ……

    3、展示图片 ,用SimpleDraweeView
    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:fresco="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.facebook.drawee.view.SimpleDraweeView        android:id="@+id/image"        android:layout_width="match_parent"        android:layout_height="200dp"        android:layout_margin="10dp"        fresco:roundAsCircle="true"        fresco:placeholderImage="@mipmap/ic_launcher" /></LinearLayout>如上:注意要选用fresco: 里的属性,需要导
    xmlns:fresco="http://schemas.android.com/apk/res-auto"属性介绍展示:
    必须声明 android:layout_width 和 android:layout_height,否则将无法正确加载图像,另外不支持 wrap_content 属性,但也有例外的时候,例外不在此阐述
    fresco:fadeDuration="300"//渐进时间  
    fresco:failureImage="@drawable/error"//失败时显示的图片 
    fresco:failureImageScaleType="centerInside"//失败图片的显示方
    fresco:retryImage="@drawable/retrying"//重试图 
    fresco:retryImageScaleType="centerCrop"//重试图显示方式 
    fresco:actualImageScaleType="focusCrop" // 设置图片缩放. 通常使用focusCrop,该属性值会通过算法把人头像放在中间
    fresco:progressBarImage="@drawable/progress_bar"// 提示用户正在加载,和加载进度无关
    fresco:roundAsCircle="false"// 是不是设置圆圈
    fresco:roundedCornerRadius="1dp"// 圆角角度,180的时候会变成圆形图片
    更多属性没再研究,到这看看他这些属性,就迫不及待想试试……
    4.实例化控件:
    SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.image);
    draweeView.setImageURI(url);显示 地址图片

    大家也许添加控件的时候会出现如下:
    GenericDraweeView  
    DraweeView
    后面会继续补上

    0 0