使用的網(wǎng)址:
http://www.itdecent.cn/p/1490965760c9
ImageLoad是現(xiàn)在在Android開發(fā)過程中廣泛使用的開源圖片加載框架,該項(xiàng)目的Git源碼:https://github.com/nostra13/Android-Universal-Image-Loader,ImageLoad的具有以下特點(diǎn):
1.多線程下載圖片,圖片可以來源于網(wǎng)絡(luò),文件系統(tǒng),項(xiàng)目文件夾assets中以及drawable中等;
2.支持隨意的配置ImageLoader,例如線程池,圖片下載器,內(nèi)存緩存策略,硬盤緩存策略,圖片顯示選項(xiàng)以及其他的一些配置;
3.支持圖片的內(nèi)存緩存,文件系統(tǒng)緩存或者SD卡緩存;
4.支持圖片下載過程的監(jiān)聽;
5.根據(jù)控件(ImageView)的大小對(duì)Bitmap進(jìn)行裁剪,減少Bitmap占用過多的內(nèi)存;
6.較好的控制圖片的加載過程,例如暫停圖片加載,重新開始加載圖片,一般使用在ListView,GridView中,滑動(dòng)過程中暫停加載圖片,停止滑動(dòng)的時(shí)候去加載圖片;
7.提供在較慢的網(wǎng)絡(luò)下對(duì)圖片進(jìn)行加載
ImageLoad的具體使用方法:
1.新建一個(gè)Android項(xiàng)目,下載JAR包添加到工程libs目錄下
2.新建一個(gè)MyApplication繼承Application,并在onCreate()中創(chuàng)建ImageLoader的配置參數(shù),并初始化到ImageLoader中代碼如下:
package com.example.uil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//創(chuàng)建默認(rèn)的ImageLoader配置參數(shù)
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(this);
//Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(configuration);
}
}
ImageLoaderConfiguration是圖片加載器ImageLoader的配置參數(shù),使用了建造者模式,這里是直接使用了createDefault()方法創(chuàng)建一個(gè)默認(rèn)的ImageLoaderConfiguration,當(dāng)然我們還可以自己設(shè)置ImageLoaderConfiguration,設(shè)置如下:
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // 內(nèi)存緩存圖片的最大寬高,默認(rèn)為屏幕寬高
.diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) // 磁盤緩存圖片的設(shè)置
.taskExecutor(...) // 緩存線程池
.taskExecutorForCachedImages(...) // 下載緩存圖片的線程池
.threadPoolSize(3) // 線程池?cái)?shù),默認(rèn)為3
.threadPriority(Thread.NORM_PRIORITY - 1) // 線程優(yōu)先級(jí)
.tasksProcessingOrder(QueueProcessingType.FIFO) // 下載和顯示的工作隊(duì)列排序
.denyCacheImageMultipleSizesInMemory() // 緩存顯示不同大小的同一張圖片
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) // 指定內(nèi)存緩存的實(shí)現(xiàn)方式
.memoryCacheSize(2 * 1024 * 1024) // 設(shè)置內(nèi)存緩存的最大字節(jié)
.memoryCacheSizePercentage(13) // 設(shè)置內(nèi)存緩存最大大小占當(dāng)前應(yīng)用可用內(nèi)存的百分比
.diskCache(new UnlimitedDiscCache(cacheDir)) // 指定磁盤緩存的實(shí)現(xiàn)方式
.diskCacheSize(50 * 1024 * 1024) // 設(shè)置磁盤緩存的最大字節(jié)
.diskCacheFileCount(100) // 磁盤緩存文件數(shù)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // 磁盤緩存文件的命名規(guī)則
.imageDownloader(new BaseImageDownloader(context)) // 設(shè)置圖片下載器,負(fù)責(zé)從圖片的各個(gè)來源獲取輸入流
.imageDecoder(new BaseImageDecoder()) // 設(shè)置圖片解碼器,負(fù)責(zé)將圖片輸入流InputStream轉(zhuǎn)換為Bitmap對(duì)象
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // 缺省的圖片顯示的配置項(xiàng)
.writeDebugLogs() // 打印debug log
.build();
上面的這些就是所有的選項(xiàng)配置,我們?cè)陧?xiàng)目中不需要每一個(gè)都自己設(shè)置,一般使用createDefault()創(chuàng)建的ImageLoaderConfiguration就能使用,然后調(diào)用ImageLoader的init()方法將ImageLoaderConfiguration參數(shù)傳遞進(jìn)去,ImageLoader使用單例模式。
3.配置Android Manifest文件
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
<!-- Include next permission if you want to allow UIL to cache images on SD card -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<application android:name="MyApplication"> ... </application> </manifest>
4.加載圖片
ImageLader提供了幾個(gè)圖片加載的方法,主要是這幾個(gè)displayImage(), loadImage(),loadImageSync(),loadImageSync()方法是同步的,android4.0有個(gè)特性,網(wǎng)絡(luò)操作不能在主線程,所以loadImageSync()方法我們就不去使用
1.loadImage()方法來加載網(wǎng)絡(luò)圖片
final ImageView mImageView = (ImageView) findViewById(R.id.image);
String imageUrl = "http://img.showcdn.cn/uploads/allimg/160307/20-16030G15J1435.jpg";
ImageLoader.getInstance().loadImage(imageUrl, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
//開始加載的時(shí)候回調(diào)
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
//加載失敗的時(shí)候回調(diào)
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//加載完成的時(shí)候回調(diào)
mImageView.setImageBitmap(loadedImage);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
//取消加載的時(shí)候回調(diào)
}
});
傳入圖片的url和ImageLoaderListener, 在回調(diào)方法onLoadingComplete()中將loadedImage設(shè)置到ImageView上面就行了,如果你覺得傳入ImageLoaderListener太復(fù)雜了,我們可以使用SimpleImageLoadingListener類,該類提供了ImageLoaderListener接口方法的空實(shí)現(xiàn),使用的是缺省適配器模式
final ImageView mImageView = (ImageView) findViewById(R.id.image);
String imageUrl = "http://img.showcdn.cn/uploads/allimg/160307/20-16030G15J1435.jpg";
ImageLoader.getInstance().loadImage(imageUrl, new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
}
});
上面只是很簡(jiǎn)單的使用ImageLoader來加載網(wǎng)絡(luò)圖片,在實(shí)際的開發(fā)中,我們并不會(huì)這么使用,那我們平常會(huì)怎么使用呢?我們會(huì)用到DisplayImageOptions,他可以配置一些圖片顯示的選項(xiàng),比如圖片在加載中ImageView顯示的圖片,是否需要使用內(nèi)存緩存,是否需要使用文件緩存等等,可供我們選擇的配置如下
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // 加載未完成時(shí)顯示的自定義圖片
.showImageForEmptyUri(R.drawable.ic_empty) // 鏈接為空時(shí)的占位圖
.showImageOnFail(R.drawable.ic_error) // 加載失敗時(shí)顯示自定義圖片
.resetViewBeforeLoading(false) // 在加載前是否重置 view ,默認(rèn)為false
.delayBeforeLoading(1000) //設(shè)置在開始加載前的延遲時(shí)間,單位為毫秒
.cacheInMemory(false) // 是否啟用內(nèi)存緩存,默認(rèn)為false
.cacheOnDisk(false) // 是否啟用磁盤緩存,默認(rèn)為false
.preProcessor(...) // 緩存在內(nèi)存之前的處理程序
.postProcessor(...) // 緩存在內(nèi)存之后的處理程序
.extraForDownloader(...) // 下載器需要的輔助信息
.considerExifParams(false) // 是否考慮圖片的 EXIF 信息,默認(rèn)為false
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // 圖片的縮放類型
.bitmapConfig(Bitmap.Config.ARGB_8888) // 圖片的色彩格式
.decodingOptions(...) // 為 BitmapFactory.Options,用于得到圖片尺寸等信息
.displayer(new SimpleBitmapDisplayer()) // 圖片的顯示方式
.handler(new Handler()) // handler 對(duì)象,消息處理
.build();
Demo--------------------
package com.example.liuentong20171024recyclerview_catcherror;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Handler;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import java.io.File;
/**
* 作者:author
* 時(shí)間 :2017/10/24:8:16
* 說明:
*/
public class MyApp extends Application {
ImageLoader imageLoader;
@Override
public void onCreate() {
super.onCreate();
File file = this.getCacheDir();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 加載未完成時(shí)顯示的自定義圖片
.showImageForEmptyUri(R.mipmap.ic_launcher) // 鏈接為空時(shí)的占位圖
.showImageOnFail(R.mipmap.ic_launcher_round) // 加載失敗時(shí)顯示自定義圖片
.resetViewBeforeLoading(false) // 在加載前是否重置 view ,默認(rèn)為false
.delayBeforeLoading(1000) //設(shè)置在開始加載前的延遲時(shí)間,單位為毫秒
.cacheInMemory(true) // 是否啟用內(nèi)存緩存,默認(rèn)為false
.cacheOnDisk(true) // 是否啟用磁盤緩存,默認(rèn)為false
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // 圖片的縮放類型
.bitmapConfig(Bitmap.Config.ARGB_8888) // 圖片的色彩格式
.handler(new Handler()) // handler 對(duì)象,消息處理
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this)
// max width, max height,即保存的每個(gè)緩存文件的最大長(zhǎng)寬
.memoryCacheExtraOptions(480, 800)
// 線程池內(nèi)加載的數(shù)量
.threadPoolSize(3)
// 線程優(yōu)先級(jí)
.threadPriority(Thread.NORM_PRIORITY - 2)
.defaultDisplayImageOptions(options)
// You can pass your own memory cache implementation你可以通過自己的內(nèi)存緩存實(shí)現(xiàn)
// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// .memoryCacheSize(2 * 1024 * 1024)
//硬盤緩存50MB
.diskCacheSize(50 * 1024 * 1024)
//將保存的時(shí)候的URI名稱用MD5
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
// 加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//將保存的時(shí)候的URI名稱用HASHCODE加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCacheFileCount(100) //緩存的File數(shù)量
.diskCache(new UnlimitedDiscCache(file))// 自定義緩存路徑
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
// .imageDownloader(new BaseImageDownloader(context, 5 * 1000,
// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超時(shí)時(shí)間
.writeDebugLogs() // Remove for release app
.build();
ImageLoader.getInstance().init(config);
}
}
圖片的二次采樣----
package code.feihu.com.shangfeihu_yuekao.utils;
import android.graphics.BitmapFactory;
/**
* Created by
* Emali: 210666592@qq.com
* 類作用:
*/
public class BitmapUtils {
/**
* @param filePath 要加載的圖片路徑
* @param destWidth 顯示圖片的控件寬度
* @param destHeight 顯示圖片的控件的高度
* @return
*/
public static android.graphics.Bitmap getBitmap(String filePath, int destWidth, int destHeight) {
//第一次采樣
BitmapFactory.Options options = new BitmapFactory.Options();
//該屬性設(shè)置為true只會(huì)加載圖片的邊框進(jìn)來,并不會(huì)加載圖片具體的像素點(diǎn)
options.inJustDecodeBounds = true;
//第一次加載圖片,這時(shí)只會(huì)加載圖片的邊框進(jìn)來,并不會(huì)加載圖片中的像素點(diǎn)
BitmapFactory.decodeFile(filePath, options);
//獲得原圖的寬和高
int outWidth = options.outWidth;
int outHeight = options.outHeight;
//定義縮放比例
int sampleSize = 1;
while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {
//如果寬高的任意一方的縮放比例沒有達(dá)到要求,都繼續(xù)增大縮放比例
//sampleSize應(yīng)該為2的n次冪,如果給sampleSize設(shè)置的數(shù)字不是2的n次冪,那么系統(tǒng)會(huì)就近取值
sampleSize *= 2;
}
/********************************************************************************************/
//至此,第一次采樣已經(jīng)結(jié)束,我們已經(jīng)成功的計(jì)算出了sampleSize的大小
/********************************************************************************************/
//二次采樣開始
//二次采樣時(shí)我需要將圖片加載出來顯示,不能只加載圖片的框架,因此inJustDecodeBounds屬性要設(shè)置為false
options.inJustDecodeBounds = false;
//設(shè)置縮放比例
options.inSampleSize = sampleSize;
//加載圖片并返回
return BitmapFactory.decodeFile(filePath, options);
}
public static android.graphics.Bitmap getBitmap(byte[] filePath, int destWidth, int destHeight) {
//第一次采樣
BitmapFactory.Options options = new BitmapFactory.Options();
//該屬性設(shè)置為true只會(huì)加載圖片的邊框進(jìn)來,并不會(huì)加載圖片具體的像素點(diǎn)
options.inJustDecodeBounds = true;
//第一次加載圖片,這時(shí)只會(huì)加載圖片的邊框進(jìn)來,并不會(huì)加載圖片中的像素點(diǎn)
BitmapFactory.decodeByteArray(filePath, 0, filePath.length, options);
//獲得原圖的寬和高
int outWidth = options.outWidth;
int outHeight = options.outHeight;
//定義縮放比例
int sampleSize = 1;
while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {
//如果寬高的任意一方的縮放比例沒有達(dá)到要求,都繼續(xù)增大縮放比例
//sampleSize應(yīng)該為2的n次冪,如果給sampleSize設(shè)置的數(shù)字不是2的n次冪,那么系統(tǒng)會(huì)就近取值
sampleSize *= 2;
}
/********************************************************************************************/
//至此,第一次采樣已經(jīng)結(jié)束,我們已經(jīng)成功的計(jì)算出了sampleSize的大小
/********************************************************************************************/
//二次采樣開始
//二次采樣時(shí)我需要將圖片加載出來顯示,不能只加載圖片的框架,因此inJustDecodeBounds屬性要設(shè)置為false
options.inJustDecodeBounds = false;
//設(shè)置縮放比例
options.inSampleSize = sampleSize;
//加載圖片并返回
return BitmapFactory.decodeByteArray(filePath, 0, filePath.length, options);
}
}
demo---
OkHttpUtils.getInstance().doGet(irb.getStories().get(position).getImages().get(0), new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final Bitmap bitmap = BitmapUtils.getBitmap(response.body().bytes(), 100, 200);
handler.post(new Runnable() {
@Override
public void run() {
holder.iv.setImageBitmap(bitmap);
}
});
}
});