FrescoImageView的使用
Fresco这是一个很实用的图片加载框架,简单方便,缓存实用,而且它的各种配置都是可以在xml里面完成,个人感觉比Gilde好用,只是在加载GIF的缩略图时不太方便,不过现在的图片大部分存在云盘上,有很多转换的方法,可以转换成png的格式。
详情进入Fresco官网地址
com.facebook.fresco:animated-gif:1.0.0
com.facebook.fresco:fresco:0.14.1
下面是我写的一个工具类
public class FrescoConfig {
private static final int MAX_HEAP_SIZE = (int) Runtime.getRuntime().maxMemory();//分配的可用内存
public static final int MAX_MEMORY_CACHE_SIZE = MAX_HEAP_SIZE / 12;//使用的缓存数量
public static final int MAX_SMALL_DISK_VERYLOW_CACHE_SIZE = 20 * ByteConstants.MB;//小图极低磁盘空间缓存的最大值(特性:可将大量的小图放到额外放在另一个磁盘空间防止大图占用磁盘空间而删除了大量的小图)
public static final int MAX_SMALL_DISK_LOW_CACHE_SIZE = 30 * ByteConstants.MB;//小图低磁盘空间缓存的最大值(特性:可将大量的小图放到额外放在另一个磁盘空间防止大图占用磁盘空间而删除了大量的小图)
public static final int MAX_SMALL_DISK_CACHE_SIZE = 40 * ByteConstants.MB;//小图磁盘缓存的最大值(特性:可将大量的小图放到额外放在另一个磁盘空间防止大图占用磁盘空间而删除了大量的小图)
public static final int MAX_DISK_CACHE_VERYLOW_SIZE = 20 * ByteConstants.MB;//默认图极低磁盘空间缓存的最大值
public static final int MAX_DISK_CACHE_LOW_SIZE = 40 * ByteConstants.MB;//默认图低磁盘空间缓存的最大值
public static final int MAX_DISK_CACHE_SIZE = 80 * ByteConstants.MB;//默认图磁盘缓存的最大值
private static final String IMAGE_PIPELINE_SMALL_CACHE_DIR = "hnImageCache";//小图所放路径的文件夹名
private static final String IMAGE_PIPELINE_CACHE_DIR = "hnImageCacheDe";//默认图所放路径的文件夹名
private static ImagePipelineConfig sImagePipelineConfig;
public static ImagePipelineConfig getImagePipelineConfig(Context context) {
if (sImagePipelineConfig == null) {
sImagePipelineConfig = configureCaches(context);
}
return sImagePipelineConfig;
}
/**
* Configures disk and memory cache not to exceed common limits
*/
private static ImagePipelineConfig configureCaches(Context context) {
ImagePipelineConfig.Builder configBuilder = null;
// 当内存紧张时采取的措施
MemoryTrimmableRegistry memoryTrimmableRegistry = NoOpMemoryTrimmableRegistry.getInstance();
memoryTrimmableRegistry.registerMemoryTrimmable(new MemoryTrimmable() {
@Override
public void trim(MemoryTrimType trimType) {
final double suggestedTrimRatio = trimType.getSuggestedTrimRatio();
if (MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio() == suggestedTrimRatio
|| MemoryTrimType.OnSystemLowMemoryWhileAppInBackground.getSuggestedTrimRatio() == suggestedTrimRatio
|| MemoryTrimType.OnSystemLowMemoryWhileAppInForeground.getSuggestedTrimRatio() == suggestedTrimRatio
) {
// 清除内存缓存
ImagePipeline imagePipeline = Fresco.getImagePipeline();
imagePipeline.clearMemoryCaches(); //内存,包括已解码和未解码
imagePipeline.clearDiskCaches(); //删除文件
imagePipeline.clearCaches();
}
}
});
if (PermissionHelper.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams(
FrescoConfig.MAX_MEMORY_CACHE_SIZE, // Max total size of elements in the cache
Integer.MAX_VALUE, // Max entries in the cache
FrescoConfig.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
//修改内存图片缓存数量
Supplier mSupplierMemoryCacheParams = new Supplier() {
@Override
public MemoryCacheParams get() {
return bitmapCacheParams;
}
};
//小图片的磁盘配置
DiskCacheConfig diskSmallCacheConfig = DiskCacheConfig.newBuilder(context)
.setBaseDirectoryPath(HnFileUtils.getExternalCacheDir(context))
.setBaseDirectoryName(IMAGE_PIPELINE_SMALL_CACHE_DIR)
.setMaxCacheSize(FrescoConfig.MAX_SMALL_DISK_CACHE_SIZE)
.setMaxCacheSizeOnLowDiskSpace(MAX_SMALL_DISK_LOW_CACHE_SIZE)
.setMaxCacheSizeOnVeryLowDiskSpace(MAX_SMALL_DISK_VERYLOW_CACHE_SIZE)
.build();
//默认图片的磁盘配置
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(context)
.setBaseDirectoryPath(Environment.getExternalStorageDirectory().getAbsoluteFile())
.setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR)
.setMaxCacheSize(FrescoConfig.MAX_DISK_CACHE_SIZE)
.setMaxCacheSizeOnLowDiskSpace(MAX_DISK_CACHE_LOW_SIZE)
.setMaxCacheSizeOnVeryLowDiskSpace(MAX_DISK_CACHE_VERYLOW_SIZE)
.build();
//缓存图片配置
configBuilder = ImagePipelineConfig.newBuilder(context)
.setBitmapMemoryCacheParamsSupplier(mSupplierMemoryCacheParams)
.setMainDiskCacheConfig(diskCacheConfig)
.setDownsampleEnabled(true) // 对图片进行自动缩放
.setResizeAndRotateEnabledForNetwork(true) // 对图片进行自动缩放
.setBitmapsConfig(Bitmap.Config.RGB_565) // //图片设置RGB_565,减小内存开销 fresco默认情况下是RGB_8888
.setMemoryTrimmableRegistry(memoryTrimmableRegistry)
.setSmallImageDiskCacheConfig(diskSmallCacheConfig);
} else {
configBuilder = ImagePipelineConfig.newBuilder(context)
.setDownsampleEnabled(true) // 对图片进行自动缩放
.setResizeAndRotateEnabledForNetwork(true) // 对图片进行自动缩放
.setBitmapsConfig(Bitmap.Config.RGB_565) // //图片设置RGB_565,减小内存开销 fresco默认情况下是RGB_8888
.setMemoryTrimmableRegistry(memoryTrimmableRegistry);
}
return configBuilder.build();
}
/**
* 加载大图
*
* @param url
* @return
*/
public static DraweeController getController(String url) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setImageUri(url))
.setProgressiveRenderingEnabled(true)
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
// .setUri(HnUrl.setImageUri(url))
.setImageRequest(request)
// .setAutoPlayAnimations(true)
// .setOldController(draweeView.getController())
// .setTapToRetryEnabled(true)//加载失败时是否允许重试
// .setControllerListener(new BaseControllerListener())
.build();
return draweeController;
}
/**
* 加载大图
*
* @param url
* @return
*/
public static DraweeController getCompressController(String url) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setCompressImageUri(url))
.setProgressiveRenderingEnabled(true)
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return draweeController;
}
public static DraweeController getControllerBanner(String url) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setImageUriBig(url))
.setProgressiveRenderingEnabled(true)
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 280),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 280)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
// .setAutoPlayAnimations(true)
.build();
return draweeController;
}
/**
* 加载小图
*
* @param url
* @return
*/
public static DraweeController getHeadController(String url) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setImageUri(url))
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 100),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 100)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
// .setAutoPlayAnimations(true)
.build();
return draweeController;
}
/**
* 加载高斯模糊
*
* @param url
* @return
*/
public static AbstractDraweeController getBlurController(String url) {
try {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setImageUri(url))
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320)))
.setPostprocessor(new IterativeBoxBlurPostProcessor(6, 10))
.build();
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return controller;
} catch (Exception e) {
}
return null;
}
/**
* 加载高斯模糊
*
* @param url
* @return
*/
public static AbstractDraweeController getBlurMsgController(String url) {
try {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(NetConstant.setImageUri(url))
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320),
HnDimenUtil.dp2px(HnBaseApplication.getContext(), 320)))
.setPostprocessor(new IterativeBoxBlurPostProcessor(8, 12))
.build();
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return controller;
} catch (Exception e) {
}
return null;
}
/**
* 加载高斯模糊
*
* @param id
* @return
*/
public static AbstractDraweeController getBlurController(int id) {
try {
ImageRequest request = ImageRequestBuilder.newBuilderWithResourceId(id)
.setPostprocessor(new IterativeBoxBlurPostProcessor(4, 8))
.build();
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return controller;
} catch (Exception e) {
}
return null;
}
/**
* 加载高斯模糊
*
* @return
*/
public static AbstractDraweeController getBlurFileController(String url) {
try {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse("file://" + url))
.setPostprocessor(new IterativeBoxBlurPostProcessor(8, 12))
.build();
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return controller;
} catch (Exception e) {
}
return null;
}
/**
* 加载本地图
*
* @param url
* @return
*/
public static DraweeController getControllerLocal(String url) {
if (TextUtils.isEmpty(url)) url = "1111";
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse("file://" + url))
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 360), HnDimenUtil.dp2px(HnBaseApplication.getContext(), 360)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return draweeController;
}
/**
* 加载本地图
*
* @return
*/
public static DraweeController getControllerResourceId(int id) {
// mIvHeader.setImageURI(Uri.parse("res://" + mContext.getPackageName() + "/" + R.drawable.login_logo));
ImageRequest request = ImageRequestBuilder.newBuilderWithResourceId(id)
.setResizeOptions(new ResizeOptions(HnDimenUtil.dp2px(HnBaseApplication.getContext(), 360), HnDimenUtil.dp2px(HnBaseApplication.getContext(), 360)))
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.build();
return draweeController;
}
}