ImageLoader用法总结

2024-05-08 17:08
文章标签 总结 用法 imageloader

本文主要是介绍ImageLoader用法总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1,配置方法

1,在Gradle中加入依赖
compile files('libs/universal-image-loader-1.9.4.jar')
2,导入jar包 universal-image-loader-1.9.4.jar
3,代码如下
首先在Application中
ImageLoader.init(getApplicationContext());
其次
public class
ImageLoader { public static final String TAG = "--ljj-- ImageLoader"; private static DisplayImageOptions options; private static DisplayImageOptions.Builder builder; private static Context applicationContext; private static boolean isShowPicture ; public static void init(Context applicationContext) { ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(applicationContext) .threadPoolSize(Thread.NORM_PRIORITY - 2)//线程池大小 .denyCacheImageMultipleSizesInMemory()//否定高速缓存图像多大小在内存中 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//磁盘缓存文件名生成器MD5 .tasksProcessingOrder(QueueProcessingType.LIFO)//后进先出法 .threadPoolSize(3) .memoryCache(new WeakMemoryCache()) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) .denyCacheImageMultipleSizesInMemory() .writeDebugLogs()//编写调试日志 .build();//建立 com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(imageLoaderConfiguration); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.image_loading)//加载图片时显示的默认图片 .showImageForEmptyUri(R.drawable.default_error)//显示空URI的图像 .showImageOnFail(R.drawable.default_error)//显示图像失败 .cacheInMemory(true)//是否添加到内存缓存中 .cacheOnDisk(true)//是否添加到硬盘缓存中 .considerExifParams(true) .bitmapConfig(Bitmap.Config.RGB_565) .build(); builder = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.image_loading)//加载图片时显示的默认图片 .showImageForEmptyUri(R.drawable.default_error)//显示空URI的图像 .showImageOnFail(R.drawable.default_error)//显示图像失败 .cacheInMemory(true)//是否添加到内存缓存中 .cacheOnDisk(true)//是否添加到硬盘缓存中 .bitmapConfig(Bitmap.Config.RGB_565) .considerExifParams(true); ImageLoader.applicationContext = applicationContext; } /** * 普通图片加载 * * @param imageURL * @param imageView */ public static void displayImage(String imageURL, ImageView imageView) { if(isShowPicture == true){ imageView.setImageResource(R.drawable.default_error); }else{ com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(urlTransformation(imageURL), imageView, options); } } /** * 普通图片加载 * * @param image 图片地址 * @param imageView 控件 */ public static void displayImage(int image, ImageView imageView) { displayImage("drawable://" + image, imageView); } /** * 圆角图片加载 * * @param image 图片地址 * @param imageView 控件 */ public static void displayImage(int image, ImageView imageView, int cornerRadiusPixels) { displayImage("drawable://" + image, imageView, cornerRadiusPixels); } /** * 圆角图片加载 * * @param imageURL 图片地址 * @param imageView 控件 * @param cornerRadiusPixels 圆角直径(控件宽或高) */ public static void displayImage(String imageURL, ImageView imageView, int cornerRadiusPixels) { builder.displayer(new RoundedBitmapDisplayer(Math.round(applicationContext.getResources().getDisplayMetrics().density * cornerRadiusPixels)));// com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(urlTransformation(imageURL), imageView, builder.build()); if(isShowPicture == true){ imageView.setImageResource(R.drawable.default_error); }else{ com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(urlTransformation(imageURL), imageView, builder.build()); } } /** * 高斯模糊图片加载 * * @param image 图片地址 * @param imageView 控件 */ public static void displayBlur(int image, ImageView imageView) { displayBlur("drawable://" + image, imageView); } /** * 高斯模糊图片加载 * * @param imageURL 图片地址 * @param imageView 控件 */ public static void displayBlur(String imageURL, ImageView imageView) {// com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(urlTransformation(imageURL), new BlurImageLoadingListener(imageView)); if(isShowPicture == true){ imageView.setImageResource(R.drawable.default_error); }else{ com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(urlTransformation(imageURL), new BlurImageLoadingListener(imageView)); } } /** * 水平方向模糊度 */ private static float hRadius = 3; /** * 竖直方向模糊度 */ private static float vRadius = 3; /** * 模糊迭代度 */ private static int iterations = 2; public static Bitmap createBitmap(int width, int height, Bitmap.Config config) { Bitmap bitmap = null; try { bitmap = Bitmap.createBitmap(width, height, config); } catch (OutOfMemoryError e) { while(bitmap == null) { System.gc(); System.runFinalization(); bitmap = createBitmap(width, height, config); } } return bitmap; } /** * 高斯模糊 */ public static Bitmap BoxBlurFilter(Bitmap bmp) { int width = bmp.getWidth(); int height = bmp.getHeight(); int[] inPixels = new int[width * height]; int[] outPixels = new int[width * height]; Bitmap bitmap = createBitmap(width, height, Bitmap.Config.RGB_565); bmp.getPixels(inPixels, 0, width, 0, 0, width, height); for (int i = 0; i < iterations; i++) { blur(inPixels, outPixels, width, height, hRadius); blur(outPixels, inPixels, height, width, vRadius); } blurFractional(inPixels, outPixels, width, height, hRadius); blurFractional(outPixels, inPixels, height, width, vRadius); bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);// Drawable drawable = new BitmapDrawable(bitmap); return bitmap; } private static void blur(int[] in, int[] out, int width, int height, float radius) { int widthMinus1 = width - 1; int r = (int) radius; int tableSize = 2 * r + 1; int divide[] = new int[256 * tableSize]; for (int i = 0; i < 256 * tableSize; i++) divide[i] = i / tableSize; int inIndex = 0; for (int y = 0; y < height; y++) { int outIndex = y; int ta = 0, tr = 0, tg = 0, tb = 0; for (int i = -r; i <= r; i++) { int rgb = in[inIndex + clamp(i, 0, width - 1)]; ta += (rgb >> 24) & 0xff; tr += (rgb >> 16) & 0xff; tg += (rgb >> 8) & 0xff; tb += rgb & 0xff; } for (int x = 0; x < width; x++) { out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb]; int i1 = x + r + 1; if (i1 > widthMinus1) i1 = widthMinus1; int i2 = x - r; if (i2 < 0) i2 = 0; int rgb1 = in[inIndex + i1]; int rgb2 = in[inIndex + i2]; ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff); tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16; tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8; tb += (rgb1 & 0xff) - (rgb2 & 0xff); outIndex += height; } inIndex += width; } } private static void blurFractional(int[] in, int[] out, int width, int height, float radius) { radius -= (int) radius; float f = 1.0f / (1 + 2 * radius); int inIndex = 0; for (int y = 0; y < height; y++) { int outIndex = y; out[outIndex] = in[0]; outIndex += height; for (int x = 1; x < width - 1; x++) { int i = inIndex + x; int rgb1 = in[i - 1]; int rgb2 = in[i]; int rgb3 = in[i + 1]; int a1 = (rgb1 >> 24) & 0xff; int r1 = (rgb1 >> 16) & 0xff; int g1 = (rgb1 >> 8) & 0xff; int b1 = rgb1 & 0xff; int a2 = (rgb2 >> 24) & 0xff; int r2 = (rgb2 >> 16) & 0xff; int g2 = (rgb2 >> 8) & 0xff; int b2 = rgb2 & 0xff; int a3 = (rgb3 >> 24) & 0xff; int r3 = (rgb3 >> 16) & 0xff; int g3 = (rgb3 >> 8) & 0xff; int b3 = rgb3 & 0xff; a1 = a2 + (int) ((a1 + a3) * radius); r1 = r2 + (int) ((r1 + r3) * radius); g1 = g2 + (int) ((g1 + g3) * radius); b1 = b2 + (int) ((b1 + b3) * radius); a1 *= f; r1 *= f; g1 *= f; b1 *= f; out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1; outIndex += height; } out[outIndex] = in[width - 1]; inIndex += width; } } private static int clamp(int x, int a, int b) { return (x < a) ? a : (x > b) ? b : x; } /** * 地址转换 * 把相对地址转换成实际地址 * * @param imageURL * @return */ public static String urlTransformation(String imageURL) { if (imageURL == null || imageURL.trim().isEmpty()) { return "drawable://" + R.drawable.default_error; } else if (imageURL.matches(NO_TRANSFORMATION)) { Log.e(TAG, "不转换 " + imageURL); return imageURL; } else { Log.e(TAG, "转换 " + HttpService.SERVER_ADDRESS + imageURL); return HttpService.SERVER_ADDRESS + imageURL; } } public static String FILE = "^[Ff][Ii][Ll][Ee]://[\\S]*"; public static String HTTP = "^[Hh][Tt][Tt][Pp]://[\\S]*"; public static String HTTPS = "^[Hh][Tt][Tt][Pp][Ss]://[\\S]*"; public static String CONTENT = "^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]://[\\S]*"; public static String ASSETS = "^[Aa][Ss][Ee][Tt][Ss]://[\\S]*"; public static String DRAWABLE = "^[Dd][Rr][Aa][Ww][Aa][Bb][Ll][Ee]://[\\S]*"; public static String NO_TRANSFORMATION = "(" + FILE + ")|(" + HTTP + ")|(" + HTTPS + ")|(" + CONTENT + ")|(" + ASSETS + ")|(" + FILE + ")|(" + DRAWABLE + ")"; /** * url转成drawable * 并添加到imageView * * @param url 网络图片地址 * @param imageView 图片控件 */ public static void loadImageFromNetwork(final String url, final ImageView imageView) { new Thread(new Runnable() { Drawable loadImageDrawable = loadImageFromNetwork(url); @Override public void run() { // 更新主线程UI资源 imageView.post(new Runnable() { @Override public void run() { imageView.setImageDrawable(loadImageDrawable); } }); } }).start(); //线程启动 } /** * url drawable * * @param imageUrl 图片地址 * @return */ public static Drawable loadImageFromNetwork(String imageUrl) { Drawable drawable = null; try { drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), "image.jpg"); } catch (IOException e) { Log.d("test", e.getMessage()); } return drawable; } public static boolean isShowPicture() { return isShowPicture; } public static void setIsShowPicture(boolean isShowPicture) { ImageLoader.isShowPicture = isShowPicture; } public static void onDestory(){ com.nostra13.universalimageloader.core.ImageLoader.getInstance().clearMemoryCache(); }}

最后,在需要的地方直接引用就可以
ImageLoader.displayImage(图片地址,图片控件);


2,开发过程中遇到的bug
虽然这个框架有很好的缓存机制,有效的避免了OOM的产生,一般的情况下产生OOM的概率比较小,但是并不能保证OutOfMemoryError永远不发生。在开发过程中稍不留意就可能会遇到内存溢出的问题,根据自己的开发经验,为了避免内存溢出的问题,总结几点注意。
1,在DisplayImageOptions选项中配置bitmapConfig为Bitmap.Config.RGB_565,因为默认是ARGB_8888, 使用RGB_565会比使用ARGB_8888少消耗2倍 的内存;
2,在DisplayImageOptions选项中设置.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY);
3,
protected void onDestroy() {super.onDestroy();ImageLoader.onDestory();
}


这篇关于ImageLoader用法总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/970918

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Mysql中isnull,ifnull,nullif的用法及语义详解

《Mysql中isnull,ifnull,nullif的用法及语义详解》MySQL中ISNULL判断表达式是否为NULL,IFNULL替换NULL值为指定值,NULLIF在表达式相等时返回NULL,用... 目录mysql中isnull,ifnull,nullif的用法1. ISNULL(expr) → 判

Java中的for循环高级用法

《Java中的for循环高级用法》本文系统解析Java中传统、增强型for循环、StreamAPI及并行流的实现原理与性能差异,并通过大量代码示例展示实际开发中的最佳实践,感兴趣的朋友一起看看吧... 目录前言一、基础篇:传统for循环1.1 标准语法结构1.2 典型应用场景二、进阶篇:增强型for循环2.