android Fresco使用,图片加载永不OOM

分享这个的原因:
1、应该有很多朋友在做android显示大量图片的时候出现OOM,那么Fresco,glide是一个不错的选择
2、Fresco与Glide比较而言:
Fresco入手快;Glide入手慢;
Fresco扩展性不强,但是基本APP用到的都有,特殊的就需要自定义;Glide扩展性极高,比较是google的亲儿子
Fresco如果对于多图片APP推荐使用;
Fresco使用相对Glide使用增加APK的大小;最新的Fresco已经支持油画以及androidX
3、因为我的开发的APP对图片要求比较高,我选择的Fresco,下面是我封装的Fresco工具类;代码中为什么我把加载图片写死了,是因为公司不给我时间修改,我直接在入口修改;您们使用可以把这个去除用传入的颜色值,或者图片

package com.xinyi.morefun.morefun.util;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.facebook.common.util.UriUtil;
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.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.common.RotationOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.xinyi.morefun.library.util.LogUtil;
import com.xinyi.morefun.library.util.UnitUtil;
import com.xinyi.morefun.library.util2.SystemUtil;
import com.xinyi.morefun.morefun.R;

import java.net.URL;

public class FrescoUtil {
    private static Context context;
    private static volatile FrescoUtil frescoUtil;
    private FrescoUtil(){}
    public static FrescoUtil getInstanceFor(Context ctx){
        context = ctx;
        if(frescoUtil == null){
            synchronized (FrescoUtil.class){
                if(frescoUtil == null){
                    frescoUtil = new FrescoUtil();
                }
            }
        }
        return frescoUtil;
    }

    /**
     * 設置圓角
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void roundImgAutoHeight(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,int topLeft,int topRight,int bottomRight,int bottomLeft,int width){
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setOldController(simpleDraweeView.getController())
                .setControllerListener(new ControllerListener() {
                    @Override
                    public void onSubmit(String id, Object callerContext) {

                    }

                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) simpleDraweeView.getLayoutParams();
                        params.width = width;
                        params.height = (int) ((float)imageInfo.getHeight()/width * width);
                        simpleDraweeView.setLayoutParams(params);
                    }

                    @Override
                    public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

                    }

                    @Override
                    public void onIntermediateImageFailed(String id, Throwable throwable) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {

                    }

                    @Override
                    public void onRelease(String id) {

                    }
                })
                .setUri(Uri.parse(url))
                .build();

        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadii(topLeft,topRight,bottomRight,bottomLeft))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }



    /**
     * 設置圓角
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     * @param cornersRadii
     */
    public void roundImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float cornersRadii,int width,int height){
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadius(cornersRadii))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }



    /**
     * 設置圓
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    @Deprecated
    public void circleImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.asCircle())
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setImageURI(url);
    }

    /**
     * 設置圓
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void circleImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,int wh){

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(wh, wh))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.asCircle())
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 設置圓角
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void roundImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float corners,int wh){

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(wh, wh))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadii(corners,corners,corners,corners))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 設置圓角
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     * @param topLeft
     * @param topRight
     * @param bottomRight
     * @param bottomLeft
     */
    public void roundImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float topLeft,float topRight,float bottomRight,float bottomLeft,int wh){

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(wh, wh))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadii(topLeft,topRight,bottomRight,bottomLeft))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 設置圓角
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     * @param topLeft
     * @param topRight
     * @param bottomRight
     * @param bottomLeft
     */
    public void roundImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float topLeft,float topRight,float bottomRight,float bottomLeft,int width,int height){

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadii(topLeft,topRight,bottomRight,bottomLeft))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 正常
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    @Deprecated
    public void nomalImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setImageURI(url);
    }

    /**
     * 正常
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImgByWH(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,int width,int height){

        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 正常,本地图片
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImgByFile(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float topLeft,float topRight,float bottomRight,float bottomLeft){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        int width,height;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            width = simpleDraweeView.getWidth();
            height = simpleDraweeView.getHeight();
        } else {
            width = simpleDraweeView.getMaxWidth();
            height = simpleDraweeView.getMaxHeight();
        }

        Uri uri = new Uri.Builder()
                .scheme(UriUtil.LOCAL_FILE_SCHEME)
                .path(url)
                .build();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadii(topLeft,topRight,bottomRight,bottomLeft))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 正常,本地图片
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImgByFile(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,float cornersRadii,int width,int height){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());

        Uri uri = new Uri.Builder()
                .scheme(UriUtil.LOCAL_FILE_SCHEME)
                .path(url)
                .build();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setRoundingParams(RoundingParams.fromCornersRadius(cornersRadii))
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }

    /**
     * 正常,本地图片
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImgByFile(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,int width,int height){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());

        Uri uri = new Uri.Builder()
                .scheme(UriUtil.LOCAL_FILE_SCHEME)
                .path(url)
                .build();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setRotationOptions(RotationOptions.autoRotate())
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setController(controller);
    }



    /**
     * 加载资源文件
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImgByResource(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,int url){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.mipmap.load_default)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFailureImage(R.mipmap.load_default)
                .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        simpleDraweeView.setHierarchy(hierarchy);
        Uri uri = Uri.parse("res://"+ SystemUtil.getInstanceFor(context).getPakageNmae()+"/"+url);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * 正常
     * @param simpleDraweeView
     * @param placeholderImage
     * @param failureImage
     * @param url
     */
    public void nomalImg(SimpleDraweeView simpleDraweeView, int placeholderImage, int failureImage,String url,int width,int height){
                ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) simpleDraweeView.getLayoutParams();
                params.width=width;
                params.height=height;
                simpleDraweeView.setLayoutParams(params);
                GenericDraweeHierarchyBuilder builder =
                        new GenericDraweeHierarchyBuilder(context.getResources());
                GenericDraweeHierarchy hierarchy = builder
                        .setFadeDuration(300)
                        .setPlaceholderImage(R.mipmap.load_default)
                        .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setFailureImage(R.mipmap.load_default)
                        .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                simpleDraweeView.setHierarchy(hierarchy);
                simpleDraweeView.setImageURI(url);

    }


    // 暂停图片请求
    public void imagePause() {
        Fresco.getImagePipeline().pause();
    }

    // 恢复图片请求
    public void imageResume() {
        Fresco.getImagePipeline().resume();
    }
}

你可能感兴趣的:(android Fresco使用,图片加载永不OOM)