Fresco的封装

Fresco介绍

Fresco是facebook开源的图片加载框架。

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

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

Fresco 中设计有一个叫做 Drawees 模块,方便地显示loading图,当图片不再显示在屏幕上时,及时地释放内存和空间占用。

Fresco 支持 Android2.3(API level 9) 及其以上系统。

相比其他的图片加载框架功能强大得多,如果还不了解同学可以点击上面的链接学习了解。

封装理由

Fresco功能强大。如果只是普通的使用很简单。
如果需要使用它真正强大的功能就很复杂,这是因为运用了太多的类和设计模式,导致使用起来非常麻烦,相比其他的一行代码就可以解决图片加载,Fresco就麻烦多了。所以我封装了一个ImageLoadFresco类方便我在项目中使用,现在开源出来给作为使用大家参考。

为什么Fresco配置这么复杂?
因为对整个Fresco图片加载框架 使用了MVC模式
M层上有很多的图片资源,比如占位图,loading图,error图
C层决定调用M层上哪些图
V层对应视图View层,最后绑定M/C层。所以在使用时需要配置M/C层需要很多代码。

源码和使用说明

说明

针对这个配置属性比较多的类,我采用Builder生成器模式,简化使用时候的代码量。类里面配置很多默认属性。如果需要添加新的属性也不会影响现有代码编译。同时在使用的时候不涉及具体的Fresco代码,后期如果有需求替换图片加载框架,也会方便。

使用示例代码

new ImageLoadFresco.LoadImageFrescoBuilder(mContext,view,url)
                .setIsRadius(true)//圆角配置
                .build();

源码

package licola.demo.com.huabandemo.httpUtils;

import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.AutoRotateDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/** * Created by LiCola on 2016/01/16 15:26 * 用Fresco加载图片的类 * 针对这个Demo已经配置很多默认的值 * 用构造器模式便于设置更多形式 * <p> * 使用示例: * new ImageLoadFresco.LoadImageFrescoBuilder(mContext,img_image_user,url_head) * .setIsCircle(true) * .build(); */
public class ImageLoadFresco {
    private static final String TAG = "ImageLoadFresco";

    //必要参数
    private SimpleDraweeView mSimpleDraweeView;
    private Context mContext;


    /** * 私有化的构造函数 得到builder的参数 构造对象 * * @param frescoBuilder 构造器 */
    private ImageLoadFresco(LoadImageFrescoBuilder frescoBuilder) {
        this.mContext = frescoBuilder.mContext;
        this.mSimpleDraweeView = frescoBuilder.mSimpleDraweeView;

        //初始化M层 用于配置各种显示资源
        GenericDraweeHierarchyBuilder builderM = new GenericDraweeHierarchyBuilder(mContext.getResources());

        //设置图片的缩放形式
        builderM.setActualImageScaleType(frescoBuilder.mActualImageScaleType);
        if (frescoBuilder.mActualImageScaleType == ScalingUtils.ScaleType.FOCUS_CROP) {
            builderM.setActualImageFocusPoint(new PointF(0f, 0f));
        }

        //初始化C层 用于控制图片的加载
        PipelineDraweeControllerBuilder builderC = Fresco.newDraweeControllerBuilder();

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(frescoBuilder.mUrl))
                .setResizeOptions(frescoBuilder.mResizeOptions)
                .build();
        builderC.setImageRequest(request);


        if (frescoBuilder.mPlaceHolderImage != null) {
            builderM.setPlaceholderImage(frescoBuilder.mPlaceHolderImage, ScalingUtils.ScaleType.CENTER);
        }

        if (frescoBuilder.mProgressBarImage != null) {
            Drawable progressBarDrawable = new AutoRotateDrawable(frescoBuilder.mProgressBarImage, 2000);
            builderM.setProgressBarImage(progressBarDrawable);

        }

        //设置重试图 同时就是设置支持加载视频时重试
        if (frescoBuilder.mRetryImage != null) {
            builderC.setTapToRetryEnabled(true);
            builderM.setRetryImage(frescoBuilder.mRetryImage);
        }

        if (frescoBuilder.mFailureImage != null) {
            builderM.setFailureImage(frescoBuilder.mFailureImage);
        }

        if (frescoBuilder.mBackgroundImage != null) {
            builderM.setBackground(frescoBuilder.mBackgroundImage);
        }

        if (frescoBuilder.mIsCircle) {
            builderM.setRoundingParams(RoundingParams.asCircle());
        }

        //如果圆角取默认值10 或者是已经修改过的mRadius值
        if (frescoBuilder.mIsRadius) {
            builderM.setRoundingParams(RoundingParams.fromCornersRadius(frescoBuilder.mRadius));
        }


        if (frescoBuilder.mControllerListener != null) {
            builderC.setControllerListener(frescoBuilder.mControllerListener);
        }

        mSimpleDraweeView.setHierarchy(builderM.build());
        mSimpleDraweeView.setController(builderC.build());
    }

    //构造器 作为类级内部类
    public static class LoadImageFrescoBuilder {
        //必要参数
        private Context mContext;
        private SimpleDraweeView mSimpleDraweeView;
        private String mUrl;

        //非必要参数
        private Drawable mPlaceHolderImage;//占位图
        private Drawable mProgressBarImage;//loading图
        private Drawable mRetryImage;//重试图
        private Drawable mFailureImage;//失败图
        private Drawable mBackgroundImage;//背景图

        private ScalingUtils.ScaleType mActualImageScaleType = ScalingUtils.ScaleType.CENTER_CROP;
        private boolean mIsCircle = false;//是否圆形图片
        private boolean mIsRadius = false;//是否圆角
        private float mRadius = 10;//圆角度数 默认10
        private ResizeOptions mResizeOptions = new ResizeOptions(3000, 3000);//图片的大小限制

        private ControllerListener mControllerListener;//图片加载的回调

        /** * 构造器的构造方法 传入必要参数 * * @param mContext * @param mSimpleDraweeView * @param mUrl */
        public LoadImageFrescoBuilder(Context mContext, SimpleDraweeView mSimpleDraweeView, String mUrl) {
            this.mContext = mContext;
            this.mSimpleDraweeView = mSimpleDraweeView;
            this.mUrl = mUrl;
        }

        /** * 构造器的build方法 构造真正的对象 并返回 * 构造之前需要检查 * * @return */
        public ImageLoadFresco build() {

// if (TextUtils.isEmpty(mUrl)) {
// throw new IllegalArgumentException("URL不能为空");
// }

            //不能同时设定 圆形圆角
            if (mIsCircle && mIsRadius) {
                throw new IllegalArgumentException("图片不能同时设置圆角和圆形");
            }

            return new ImageLoadFresco(this);
        }

        public LoadImageFrescoBuilder setActualImageScaleType(ScalingUtils.ScaleType mActualImageScaleType) {
            this.mActualImageScaleType = mActualImageScaleType;
            return this;
        }

        public LoadImageFrescoBuilder setPlaceHolderImage(Drawable mPlaceHolderImage) {
            this.mPlaceHolderImage = mPlaceHolderImage;
            return this;
        }

        public LoadImageFrescoBuilder setProgressBarImage(Drawable mProgressBarImage) {
            this.mProgressBarImage = mProgressBarImage;
            return this;
        }

        public LoadImageFrescoBuilder setRetryImage(Drawable mRetryImage) {
            this.mRetryImage = mRetryImage;
            return this;
        }

        public LoadImageFrescoBuilder setFailureImage(Drawable mFailureImage) {
            this.mFailureImage = mFailureImage;
            return this;
        }

        public LoadImageFrescoBuilder setBackgroundImage(Drawable mBackgroundImage) {
            this.mBackgroundImage = mBackgroundImage;
            return this;
        }

        public LoadImageFrescoBuilder setBackgroupImageColor(int colorId) {
            Drawable color = mContext.getResources().getDrawable(colorId);
            this.mBackgroundImage = color;
            return this;
        }

        public LoadImageFrescoBuilder setIsCircle(boolean mIsCircle) {
            this.mIsCircle = mIsCircle;
            return this;
        }

        public LoadImageFrescoBuilder setIsRadius(boolean mIsRadius) {
            this.mIsRadius = mIsRadius;
            return this;
        }

        public LoadImageFrescoBuilder setIsRadius(boolean mIsRadius, float mRadius) {
            this.mRadius = mRadius;
            this.mIsRadius = mIsRadius;
            return this;
        }

        public LoadImageFrescoBuilder setRadius(float mRadius) {
            this.mRadius = mRadius;
            return this;
        }

        public LoadImageFrescoBuilder setResizeOptions(ResizeOptions mResizeOptions) {
            this.mResizeOptions = mResizeOptions;
            return this;
        }

        public LoadImageFrescoBuilder setControllerListener(ControllerListener mControllerListener) {
            this.mControllerListener = mControllerListener;
            return this;
        }

    }
}

你可能感兴趣的:(开源,图片,生成器模式,Fresco)