安卓快速搭建Fresco网络加载框架

不得不说Fresco框架的强大,尤其喜欢fresco的SimpleDraweeView,可以定义很多加载效果,而且设置显示圆形头像这样的需求都很简单,下面是我集成Fresco代码的整理。
第一步添加依赖:

implementation 'com.facebook.fresco:fresco:0.9.0'

第二步创建ImagePipelineConfig:

public class ImagePipelineConfigFactory {
    private static final String IMAGE_PIPELINE_CACHE_DIR = "imageCache";

    private static ImagePipelineConfig sImagePipelineConfig;
    private static final int MAX_HEAP_SIZE = (int) Runtime.getRuntime().maxMemory();

    public static final int MAX_DISK_CACHE_SIZE = 300 * ByteConstants.MB;
    public static final int MAX_MEMORY_CACHE_SIZE = MAX_HEAP_SIZE / 3;

    /**
     * Creates config using android http stack as network backend.
     */
    public static ImagePipelineConfig getImagePipelineConfig(Context context) {
        if (sImagePipelineConfig == null) {
            ImagePipelineConfig.Builder configBuilder = ImagePipelineConfig.newBuilder(context);
            configureCaches(configBuilder, context);
            configBuilder.setBitmapsConfig(Bitmap.Config.RGB_565);
            configBuilder.setDownsampleEnabled(true);
            sImagePipelineConfig = configBuilder.build();
        }
        return sImagePipelineConfig;
    }

    /**
     * Configures disk and memory cache not to exceed common limits
     */
    private static void configureCaches(ImagePipelineConfig.Builder configBuilder, Context context) {
        final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams(
                MAX_MEMORY_CACHE_SIZE, // Max total size of elements in the cache
                Integer.MAX_VALUE,                     // Max entries in the cache
                MAX_MEMORY_CACHE_SIZE, // Max total size of elements in eviction queue
                Integer.MAX_VALUE,                     // Max length of eviction queue
                Integer.MAX_VALUE);                    // Max cache entry size
        configBuilder
                .setBitmapMemoryCacheParamsSupplier(
                        new Supplier() {
                            public MemoryCacheParams get() {
                                return bitmapCacheParams;
                            }
                        })
                .setMainDiskCacheConfig(DiskCacheConfig.newBuilder(context)
                        .setBaseDirectoryPath(getExternalCacheDir(context))
                        .setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR)
                        .setMaxCacheSize(MAX_DISK_CACHE_SIZE)
                        .build());
    }

    public static File getExternalCacheDir(final Context context) {
        return context.getExternalCacheDir();
    }

    public static boolean hasExternalCacheDir() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    public static File createFile(String folderPath, String fileName) {
        File destDir = new File(folderPath);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        return new File(folderPath, fileName);
    }

}

第三步Application初始化:

Fresco.initialize(this, ImagePipelineConfigFactory.getImagePipelineConfig(this));

第四步开始使用:


SimpleDraweeView此控件属性比较多,不熟悉的童鞋可以去官方文档查询传送带

工具类ImageLoaderUtils:

 public static void setImage(SimpleDraweeView imageView, String uriStr) {
        String uriPath = null;
        if (uriStr.startsWith("assets://")) {
            uriPath = uriStr.replaceAll("assets://", "asset:/");
        } else if (uriStr.startsWith("file:///")) {
            uriPath = uriStr.replace("file:///", "file:/");
        } else {
            uriPath = uriStr;
        }
        Uri uri = Uri.parse(uriPath);

        int width = 500;
        int height = 500;
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();

        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(imageView.getController())
                .setControllerListener(new BaseControllerListener())
                .build();
        imageView.setController(controller);
    }

只要传入SimpleDraweeView和加载图片的地址就ok了。

你可能感兴趣的:(安卓快速搭建Fresco网络加载框架)