@Override public void onCreate() { super.onCreate(); OkHttpClient okHttpClient = new OkHttpClient.Builder() .build(); ImagePipelineConfig config = OkHttpImagePipelineConfigFactory.newBuilder(this, okHttpClient) .setMainDiskCacheConfig(getDiskCacheConfig()) .setNetworkFetcher(new OkHttpNetworkFetcher(okHttpClient)) .setDownsampleEnabled(true) .build(); Fresco.initialize(this, config); Context context = getApplicationContext(); String packageName = context.getPackageName(); String processName = getProcessName(android.os.Process.myPid()); CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context); strategy.setUploadProcess(processName == null || processName.equals(packageName)); CrashReport.initCrashReport(getApplicationContext(), "0a6e92fb70", false, strategy); registerActivityLifecycleCallbacks(ActivityLifecycleHelper.build()); }
public static ImagePipelineConfig.Builder getConfigBuilder(Context context, File baseDirectoryPath, OkHttpClient okHttpClient) { DiskCacheConfig smallDiskCacheConfig = DiskCacheConfig .newBuilder(context).setBaseDirectoryPath(baseDirectoryPath) .setBaseDirectoryName(IMAGE_PIPELINE_SMALL_CACHE_DIR) .setMaxCacheSize(MAX_DISK_CACHE_SIZE) .setMaxCacheSizeOnLowDiskSpace(MAX_DISK_CACHE_LOW_SIZE) .setMaxCacheSizeOnVeryLowDiskSpace(MAX_DISK_CACHE_VERYLOW_SIZE) .build(); DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(context) .setBaseDirectoryPath(baseDirectoryPath)//缓存图片基路径 .setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR)//文件夹名 .setMaxCacheSize(MAX_DISK_CACHE_SIZE)//默认缓存的最大大小。 .setMaxCacheSizeOnLowDiskSpace(MAX_DISK_CACHE_LOW_SIZE)//缓存的最大大小,当设备磁盘空间低时。 .setMaxCacheSizeOnVeryLowDiskSpace(MAX_DISK_CACHE_VERYLOW_SIZE)//缓存的最大大小,当设备磁盘空间极低时。 .build(); OkHttpNetworkFetcher networkFetcher = new OkHttpNetworkFetcher(okHttpClient); return ImagePipelineConfig.newBuilder(context) .setNetworkFetcher(networkFetcher)//自定的网络层配置:如OkHttp,Volley .setCacheKeyFactory(LJCacheKeyFactory .getInstance())//缓存Key工厂 .setBitmapMemoryCacheParamsSupplier(new LJBitmapMemoryCacheSupplier((ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE))) //内存缓存配置(一级缓存,已解码的图片) .setMainDiskCacheConfig(diskCacheConfig)//磁盘缓存配置(总,三级缓存) .setSmallImageDiskCacheConfig(smallDiskCacheConfig);//磁盘缓存配置(小图片,可选~三级缓存的小图优化缓存) }
/** * 初始化fresco */ private void initFresco() { Set<RequestListener> requestListeners = new HashSet<>(); requestListeners.add(new RequestLoggingListener()); ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this) .setBitmapsConfig(Bitmap.Config.ARGB_8888) .setDownsampleEnabled(true) .setNetworkFetcher(new OkHttpNetworkFetcher(new OkHttpClient())) .setRequestListeners(requestListeners) .build(); Fresco.initialize(this, config); }