12.2-Fresco使用

Fresco概述

Fresco是Facebook提供的开源图片加载库,它能够从网络,本地存储和Android资源文件中加载图片,且具有三级缓存设计(2级内存,1级文件)。Fresco中实现了各种加载过程以及加载后的图片绘制,整体都很强大。所以准备来好好学学这个框架啦。

Fresco框架的设计主要采用的是MVC模式。DraweeView实现了View的功能,DraweeHierarchy实现了Model的功能,DraweeController实现Controller的功能。

基本使用

依赖

implementation 'com.facebook.fresco:fresco:1.12.1'

// 在 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'

初始化配置

在进行图片加载之前,需要配置Fresco类,Fresco.initialize只需要调用一次,所以我们在Application中进行初始化:

public class MyApplication extends Application {

@Override

public void onCreate(){

super.onCreate();

Fresco.initialize(this);

}

高级初始化-配置缓存文件夹 。note:注意处理sd卡权限

DiskCacheConfig diskConfig = DiskCacheConfig.newBuilder(this)

.setBaseDirectoryPath(new File("/storage/emulated/0/fresco"))

.build();

ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)

.setMainDiskCacheConfig(diskConfig)

.build();

Fresco.initialize(this, config);

网络配置

  • 网络权限配置:从网络下载图片,还需要添加网络访问权限
  • 在AndroidManifest.xml中配置MyApplication
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
...
</application>

xml声明

在xml中配置SimpleDraweeView,注意SimpleDraweeView不能使用wrap_content
添加 xmlns:fresco=“http://schemas.android.com/apk/res-auto” // 加入命名空间

<com.facebook.drawee.view.SimpleDraweeView
    xmlns:fresco="http://schemas.android.com/apk/res-auto"  // 加入命名空间
	android:id="@+id/sdv"
	android:layout_width="match_parent"
	app:placeholderImage="@mipmap/ic_launcher"
	android:layout_height="match_parent" />

加载图片

mSdv 就是图片的控件对象,直接加载图片url

private String mUrl = "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg";

mSdv.setImageURI(mUrl);

其他属性

<com.facebook.drawee.view.SimpleDraweeView

android:layout_width="20dp"

android:layout_height="20dp"

fresco:fadeDuration="300" // 淡出时间,毫秒。

fresco:actualImageScaleType="focusCrop" // 等同于android:scaleType。

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" // 背景图,不会被View遮挡。

fresco:roundAsCircle="false" // 是否是圆形图片。

fresco:roundedCornerRadius="1dp" // 四角圆角度数,如果是圆形图片,这个属性被忽略。

fresco:roundTopLeft="true" // 左上角是否圆角。

fresco:roundTopRight="false" // 右上角是否圆角。

fresco:roundBottomLeft="false" // 左下角是否圆角。

fresco:roundBottomRight="true" // 左下角是否圆角。

fresco:roundingBorderWidth="2dp" // 描边的宽度。

fresco:roundingBorderColor="@color/border_color" 描边的颜色。

/>
发布了118 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chentaishan/article/details/104876544