今天来介绍图片加载的框架Android-Universal-Image-Loader

  GITHUB上的下载路径为:https://github.com/nostra13/Android-Universal-Image-Loader

  也可以自行百度下载。

  首先来封装的一个类CacheTool ,由于其他加载图片的方法有点繁琐,所以这里仅封装了一个简单实用的加载方法:


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Environment;
import android.widget.ImageView; import java.io.File; import com.ncct.app.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader; /**
* 图片加载框架
*
* @author jiang
*
*/
public class CacheTool { private static File cacheDir = Environment.getDataDirectory();
private static DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.loading_img)
.showImageForEmptyUri(R.drawable.loading_error).showImageOnFail(R.drawable.loading_error)
.cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build(); private static ImageLoaderConfiguration config; public static void Init(Context context) {
config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(, ) // max
// width,
// max
// height,即保存的每个缓存文件的最大长宽
.discCacheExtraOptions(, , CompressFormat.JPEG, , null) // Can
// slow
// ImageLoader,
// use
// it
// carefully
// (Better
// don't
// use
// it)/设置缓存的详细信息,最好不要设置这个
.threadPoolSize()// 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - ).denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache( * * )) // You
// can
// pass
// your
// own
// memory
// cache
// implementation/你可以通过自己的内存缓存实现
.memoryCacheSize( * * ).discCacheSize( * * )
.discCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5
// 加密
.tasksProcessingOrder(QueueProcessingType.LIFO).discCacheFileCount() // 缓存的文件数量
// .discCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(new BaseImageDownloader(context, * , * )) // connectTimeout
// (5
// s),
// readTimeout
// (30
// s)超时时间
.writeDebugLogs() // Remove for release app
.build();// 开始构建
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
} /**
* 加载图片并监听回调结果
*
* @param iv
* @param url
* @param mImageLoadingListener
*/
public static void displayImg(ImageView iv, String url, ImageLoadingListener mImageLoadingListener) {
ImageLoader.getInstance().displayImage(url, iv, options, mImageLoadingListener);
} /**
* 加载图片
*
* @param iv
* @param url
*/
public static void displayImg(ImageView iv, String url) { ImageLoader.getInstance().displayImage(url, iv, options);
} /**
* 清除内存
*/
public static void clearMemoryCache() {
ImageLoader.getInstance().clearMemoryCache();
} /**
* 清除缓存
*/
public static void clearDiskCache() {
ImageLoader.getInstance().clearDiscCache();
} /**
* 得到某个图片的缓存路径
*
* @param imageUrl
* @return
*/
public static String getImagePath(String imageUrl) {
return ImageLoader.getInstance().getDiscCache().get(imageUrl).getPath();
} }

  封装好了,里面都有详细的介绍,这里介绍下上面的中的ImageLoadingListener 接口回调,按ctrl + 鼠标左键可以进入jar包里的java文件:

/*******************************************************************************
* Copyright 2011-2013 Sergey Tarasevich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.nostra13.universalimageloader.core.assist; import android.graphics.Bitmap;
import android.view.View; /**
* Listener for image loading process.<br />
* You can use {@link SimpleImageLoadingListener} for implementing only needed methods.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @see SimpleImageLoadingListener
* @see FailReason
* @since 1.0.0
*/
public interface ImageLoadingListener { /**
* Is called when image loading task was started
*
* @param imageUri Loading image URI
* @param view View for image
*/
void onLoadingStarted(String imageUri, View view); /**
* Is called when an error was occurred during image loading
*
* @param imageUri Loading image URI
* @param view View for image. Can be <b>null</b>.
* @param failReason {@linkplain FailReason The reason} why image loading was failed
*/
void onLoadingFailed(String imageUri, View view, FailReason failReason); /**
* Is called when image is loaded successfully (and displayed in View if one was specified)
*
* @param imageUri Loaded image URI
* @param view View for image. Can be <b>null</b>.
* @param loadedImage Bitmap of loaded and decoded image
*/
void onLoadingComplete(String imageUri, View view, Bitmap loadedImage); /**
* Is called when image loading task was cancelled because View for image was reused in newer task
*
* @param imageUri Loading image URI
* @param view View for image. Can be <b>null</b>.
*/
void onLoadingCancelled(String imageUri, View view);
}

  从以上代码中我们可以了解到接口中我们可以监听到开始、失败、完成、取消的动作。

  现在开始使用吧:

    private ImageView My_Head;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.personcenter);
My_Head = (ImageView) findViewById(R.id.My_Head);
String Url = "http://pic.nipic.com/2007-11-09/200711912453162_2.jpg";
CacheTool.displayImg(My_Head , Url );
}

  Mark一下,暂存一个直接通过URL获取bitmap的函数,未作内存处理。

    /**
* 获取指定路径的图片
*
* @param urlpath
* @return
* @throws Exception
*/
public Bitmap getImage(String urlpath) throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout();
Bitmap bitmap = null;
InputStream inputStream = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}

安卓图片加载框架--Universal-Image-Loader的更多相关文章

  1. 安卓图片加载框架之Glide框架

    Glide框架加载有两种,第一,是加载图片,第二是加载布局背景.首先我来说说第一种情况加载图片. Glide.with(getActivity()).load(lists.get(position). ...

  2. 一起写一个Android图片加载框架

    本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...

  3. Android中常见的图片加载框架

    图片加载涉及到图片的缓存.图片的处理.图片的显示等.而随着市面上手机设备的硬件水平飞速发展,对图片的显示要求越来越高,稍微处理不好就会造成内存溢出等问题.很多软件厂家的通用做法就是借用第三方的框架进行 ...

  4. 强大的图片加载框架Fresco的使用

    前面在卓新科技有限公司实习的时候,在自己的爱吖头条APP中,在图片异步加载的时候和ListView的滑动中,总会出现卡顿,这是因为图片的缓存做的不是足够到位,在项目监理的帮助下,有使用Xutils框架 ...

  5. 【光速使用开源框架系列】图片加载框架ImageLoader

    [关于本系列] 最近看了不少开源框架,网上的资料也非常多,但是我认为了解一个框架最好的方法就是实际使用.本系列博文就是带领大家快速的上手一些常用的开源框架,体会到其作用. 由于作者水平有限,本系列只会 ...

  6. Fresco-Facebook的图片加载框架的使用

    目前常用的开源图片加载框架有:1.Universal-Image-Loader,该项目存在于Github上面https://github.com/nostra13/Android-Universal- ...

  7. Fresco从配置到使用(最高效的图片加载框架)

    Frescoj说明:      facebook开源的针对android应用的图片加载框架,高效和功能齐全. 支持加载网络,本地存储和资源图片: 提供三级缓存(二级memory和一级internal ...

  8. Android之图片加载框架Fresco基本使用(一)

    PS:Fresco这个框架出的有一阵子了,也是现在非常火的一款图片加载框架.听说内部实现的挺牛逼的,虽然自己还没研究原理.不过先学了一下基本的功能,感受了一下这个框架的强大之处.本篇只说一下在xml中 ...

  9. Android 框架练成 教你打造高效的图片加载框架(转)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41874561,本文出自:[张鸿洋的博客] 1.概述 优秀的图片加载框架不要太多, ...

随机推荐

  1. 为什么API多用C而不是C++,为什么C++程序大多不使用异常

    读Defective C++随笔 不尽知用兵之害者,则不能尽知用兵之利也 ——<孙子兵法> 1.为什么API多用C而不是C++以前就一直很奇怪,为什么API大都用C的方式提供,即使有C++ ...

  2. 测试 Components 与 Controls 的区别(嵌套在Panel上的Edit,依然是Form1的Component)

    本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  3. 可以用变量指定大小的Bits对象实现

    c++的容器中有位对象bitset,但是个人认为最大的问题是定义是必须指定常数大小,比如 bitset<3> bit; 无法实现 int n = 3; bitset<n> bi ...

  4. .NET Core 微服务之Polly熔断策略

    紧接着上一篇说,咱们继续介绍Polly这个类库 熔断策略(Circuit-breaker) 如果调用某个目标服务出现过多超时.异常等情况,可以采取一定时间内熔断该服务的调用,熔断期间的请求将不再继续调 ...

  5. spring boot单元测试之RestTemplate(三)——api详解

    本篇内容来自翟永超的<Springcloud微服务实战>,转载请注明. 一.GET请求 在RestTemplate中,对GET请求可以通过如下两个方法进行调用实现. 第一种:getForE ...

  6. centos7PXE和cobbler自动部署装机

    安装程序启动过程MBR:isolinux/boot.catstage2: isolinux/isolinux.bin配置文件:isolinux/isolinux.cfg 每个对应的菜单选项: 加 ...

  7. 【Flink】深入理解Flink-On-Yarn模式

    1. 前言 Flink提供了两种在yarn上运行的模式,分别为Session-Cluster和Per-Job-Cluster模式,本文分析两种模式及启动流程. 下图展示了Flink-On-Yarn模式 ...

  8. Spring 入门程序

    1.0 导包的时候要注意: 以上的第一个是.class文件 以上的第二个是文件的解释性页面. 以上的第三个是.java文件 2.0 配置文件需要导入依赖(有dtd 依赖,也有xsd依赖) ²  从be ...

  9. mongoshell 执行JavaScript文件获取数据库实例基本信息

    由于MongoDB没有关系型数据中强大的数据字典.因此,如果需要汇总统计一些内部信息,包括数据量,基本元信息,集群架构,状态信息.则需要自己写脚本统计.为日后工作方便,本人在此将一些统计信息通过Jav ...

  10. 设计模式-访问者模式(Visitor)

    访问者模式是行为模式的一种.访问者模式的基本想法是,软件系统中拥有一个由许多对象构成的.比较稳定的对象结构,这些对象的类都拥有一个accept方法用来接受访问者的访问.访问者是一个接口,它拥有一个vi ...