安卓图片框架:universal-image-loader的高速使用

在安卓开发过程中难免会遇到下面几个情况:

1、图片异步载入

2、图片缓存

3、图片显示

4、其他……(忘记了)

以上的这些情况,可能要自己去写不少代码去实现这些功能。并且对于一些新手,可能写了半天,发现效果非常不理想、内存问题也处理不好、异步也不好控制(我就是这样)。所以搜了一下。发现网上最流行的一个安卓图片开源框架 universal-image-loader,亲自使用了一下,发现真是的好用啊。一共就加十几行代码就搞定了。妈妈再也不用操心我花时间去瞎搞八搞的了!

好了。以下来说说高速使用这个框架的步骤:

一、往project的libs目录中加入 universal-image-loader-1.9.2-with-sources.jar 这个Jar包。

点击这里能够直接下载哦!

二、新建一个 继承application的类。当中的代码看以下:

public class MyApplication extends Application {
	public void onCreate() {
		super.onCreate();
		initImageLoader(getApplicationContext());
	}

	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.MAX_PRIORITY).denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		ImageLoader.getInstance().init(config);
	}

}

注意。这个application类要在menifest中的application标签中注明哦。就是以下这样:

  <application

        android:name="com.lee.example.MyApplication"


好了,加入完以上两步的内容,主要的框架环境就搭建好了。以下是怎样使用它来载入网络图片:

先声明两个对象:

private DisplayImageOptions options;

private ImageLoader imageLoader;


然后在类的构造函数或者activity的increate方法中初始化它们:

imageLoader = ImageLoader.getInstance();

options = new DisplayImageOptions.Builder()

.showImageOnLoading(null)//载入过程中显示的图片

.showImageForEmptyUri(null)//载入内容为空显示的图片

.showImageOnFail(null)//载入失败显示的图片

.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)

.bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(388)).build();

以下是使用:

imageLoader.displayImage(imageFile, imageView, options);

说明:

imageFile:图片的网络路径(也能够用本地的路径哦,详情看文末的框架github中文档说明)

imageView:就是图片控件哈~

就上面这么一句。太方便了哈。



框架的github地址:https://github.com/nostra13/Android-Universal-Image-Loader


小广告:关注微信公众号:“大大花猫” ,它是一个智能聊天机器人,能够回答各种问题,包含查询各种信息哦!

它还能够进行人脸识别,仅仅要发送一张人脸照片,就能够进行识别并回复出各种信息哦!

它还有很多微信网页小游戏哦!还有很多其它精彩功能正在开发中! 快快关注吧!^ ^


原文地址:https://www.cnblogs.com/bhlsheji/p/5265571.html