Fresco是Facebook发布的一款开源框架,号称是目前最强的Android图片加载库,在内存方面的表现极为优秀,既然有如此信心,
对于Fresco的一些介绍,就不多说了,如果你感兴趣肯定会到网上找一些相关的资料,建议你到官网查看:http://frescolib.org/。
当然了,国内的大神也对这个网站进行了翻译,如果你英语水平比较捉急,请到http://fresco-cn.org/来进行观摩,不过还是建议到官网,
因为你不知道什么时候会有更新,第三方的文档会不及时。
好了,话不多说,我相信酷爱安卓的你,和我一样,迫不及待。
1)添加依赖库。
//基本依赖
compile 'com.facebook.fresco:fresco:0.14.1'
// 仅支持 WebP 静态图,需要添加
compile 'com.facebook.fresco:webpsupport:0.14.1'
// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.14.1'
// 支持 WebP 静态图及 WebP 动图,需要添加
compile 'com.facebook.fresco:animated-webp:0.14.1'
compile 'com.facebook.fresco:webpsupport:0.14.1'
// 在 API < 14 上的机器支持 WebP 时,需要添加
compile 'com.facebook.fresco:animated-base-support:0.14.1'
2)然后如果你是多次用到Fresco,建议在 Application 初始化,如果只是写着玩,就在应用调用 setContentView() 之前,进行初始化:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
记得一定要在清单文件中注册
3)因为我这里加载的是一张网络图片,所以要获得网络权限
<uses-permission android:name="android.permission.INTERNET"/>
4)用人家的框架就要按照人家的要求来是不,所以控件名字,命名空间都要听人家的
Fresco不同于其它几个图片加载库,它是基于自定义控件的,要使用控件SimpleDraweeView来显示图片,
注意,控件不支持宽高同时设置为wrap_content,默认强制设置宽高,否则不显示图片
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
tools:context="com.example.demofresco.view.FrescoSpimgActivity">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_fresco_spimg"
android:layout_width="300dp"
android:layout_height="300dp"
fresco:placeholderImage = "@mipmap/ic_launcher"
android:layout_gravity="center"/>
</LinearLayout>
5)在activity中加载图片显示,Fresco 不支持相对路径的URI. 所有的URI都必须是绝对路径,并且带上该URI的scheme。
public class FrescoSpimgActivity extends AppCompatActivity {
private SimpleDraweeView sdv_fresco_spimg;
private String PATH = "https://ws1.sinaimg.cn/large/0065oQSqgy1fxd7vcz86nj30qo0ybqc1.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fresco_spimg);
initView();
//显示要加载的图片
Uri uri = Uri.parse(PATH);
//创建Build对象
GenericDraweeHierarchyBuilder genericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder(getResources());
//设置样式
GenericDraweeHierarchy build = genericDraweeHierarchyBuilder.setProgressBarImage(new ProgressBarDrawable()).build();
sdv_fresco_spimg.setHierarchy(build);
sdv_fresco_spimg.setImageURI(uri);
}
private void initView() {
sdv_fresco_spimg = (SimpleDraweeView) findViewById(R.id.sdv_fresco_spimg);
}
}
drawee支持如下属性:
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"
//图片加载失败时显示,提示用户点击重新加载,重复加载4次还是没有加载出来的时候才会显示failureImage的图片
fresco:retryImageScaleType="centerCrop" //重新加载的图片缩放类型
fresco:progressBarImage="@drawable/progress_bar" //进度条图片
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
//进度图自动旋转间隔时间(单位:毫秒ms)
fresco:backgroundImage="@color/blue"
//背景图片,这里的背景图片首先被绘制
fresco:overlayImage="@drawable/watermark"
// 设置叠加图,在xml中只能设置一张叠加图片,如果需要多张图片的话,需要在java代码中设置哦
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" //边框颜色
上面的ImageScaleType有下面一些缩放类型:
center
居中,无缩放
centerCrop
保持宽高比缩小或放大,是的两边都大于或等于边界,且宽和高契合显示边界,居中显示
focusCrop
同centerCrop,但居中点不是中点,而是指定的某个点
centerInside
缩放图片使两边都在显示边界内,居中显示。和fitCenter不同,不会对图片进行放大。如果尺寸大于显示边界,则保持宽高比例缩小图片。
fitCenter
保持宽高比,缩小或者放大,使得图片完全显示在显示边界内,且宽或高契合显示边界,居中显示。
fitStart
同上,但不居中,和显示边界左上对齐。
fitEnd
同fitCenter,但不居中,和显示边界右下对齐。
fitXY
不保存宽高比,填充满显示边界。
none
如果要使用tile mode显示,则需要设置为none