使用 Android-Universal-Image-Loader 圖片載入框架,載入圖片
阿新 • • 發佈:2019-02-20
特點
多執行緒下載圖片,圖片可以來源於網路,檔案系統,專案資料夾assets中以及drawable中等
支援隨意的配置ImageLoader,例如執行緒池,圖片下載器,記憶體快取策略,硬碟快取策略,圖片顯示選項以及其他的一些配置
支援圖片的記憶體快取,檔案系統快取或者SD卡快取
支援圖片下載過程的監聽
根據控制元件(ImageView)的大小對Bitmap進行裁剪,減少Bitmap佔用過多的記憶體
較好的控制圖片的載入過程,例如暫停圖片載入,重新開始載入圖片,一般使用在ListView,GridView中,滑動過程中暫停載入圖片,停止滑動的時候去載入圖片
提供在較慢的網路下對圖片進行載入
使用步驟
第一步,新增依賴
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
第二步,新增許可權
<manifest>
<!-- Include following permission if you load images from Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Include following permission if you want to cache images on SD card -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
第三步,初始化並配置 Universal-Image-Loader
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//建立預設的ImageLoader配置引數
//不要將這段程式碼拷貝進你的工程。這是所有配置選項使用的範例,根據需求選擇性的配置即可。
//檢視sample工程來學習如何正確的使用ImageLoader
//這裡的路徑可以自定義
File cacheDir = StorageUtils.getCacheDirectory(this);
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
// 預設等於你的螢幕尺寸,裝置螢幕寬高
.memoryCacheExtraOptions(480, 800)
// 在將下載的圖片儲存到你的sd卡之前會重新計算,壓縮。
// 這個屬性不要濫用,只有你在對應的需求時再用,因為他會使你的ImageLoader變的很慢。
.diskCacheExtraOptions(480, 800, null)
//用於執行從源獲取圖片任務的 Executor,為configuration中的 taskExecutor,
// 如果為null,則會呼叫DefaultConfigurationFactory.createExecutor(…)根據配置返回一個預設的執行緒池。
.taskExecutor(null)
//用於執行從快取獲取圖片任務的 Executor,為configuration中的 taskExecutorForCachedImages,
// 如果為null,則會呼叫DefaultConfigurationFactory.createExecutor(…)根據配置返回一個預設的執行緒池。
.taskExecutorForCachedImages(null)
// 表示核心池大小(最大併發數) 預設為3
.threadPoolSize(3)
// 執行緒優先順序,預設Thread.NORM_PRIORITY - 2
.threadPriority(Thread.NORM_PRIORITY - 2)
// 任務程序的順序,預設為:FIFO 先進先出
.tasksProcessingOrder(QueueProcessingType.FIFO)
//設定記憶體快取不允許快取一張圖片的多個尺寸,預設允許。
.denyCacheImageMultipleSizesInMemory()
//圖片記憶體快取
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
//memoryCacheSize 為 0,則設定該記憶體快取的最大位元組數為 App 最大可用記憶體的 1/8。
.memoryCacheSize(2 * 1024 * 1024)
// 建立最大的記憶體快取百分比,預設為 13%
.memoryCacheSizePercentage(13)
// 硬碟快取路徑,預設為StorageUtils.getCacheDirectory(context)
.diskCache(new UnlimitedDiskCache(cacheDir))
//硬碟快取大小
.diskCacheSize(50 * 1024 * 1024)
//快取檔案數量
.diskCacheFileCount(100)
// 硬碟快取檔名生成器,預設為雜湊檔名生成器
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
// 建立圖片下載器,預設是BaseImageDownloader
.imageDownloader(new BaseImageDownloader(this))
// 圖片解碼器,負責將圖片輸入流InputStream轉換為Bitmap物件
.imageDecoder(new BaseImageDecoder(true))
// 圖片顯示的配置項。比如載入前、載入中、載入失敗應該顯示的佔位圖片,圖片是否需要在磁碟快取,是否需要在記憶體快取等
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
//是否顯示除錯log資訊
.writeDebugLogs()
.build();
//Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(configuration);
}
}
推薦初始化配置
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//這裡的路徑可以自定義
File cacheDir = new File(Environment.getExternalStorageDirectory()+"/target");
ImageLoaderConfiguration.Builder configuration = new ImageLoaderConfiguration.Builder(this);
configuration.threadPriority(Thread.NORM_PRIORITY - 2);
configuration.denyCacheImageMultipleSizesInMemory();
configuration.diskCacheFileNameGenerator(new Md5FileNameGenerator());
configuration.diskCacheSize(50 * 1024 * 1024); // 50M 大小
configuration.tasksProcessingOrder(QueueProcessingType.LIFO);
configuration.diskCache(new UnlimitedDiskCache(cacheDir));
//在釋出release包時,需要遮蔽UIL中的log資訊,下面這行程式碼可以做到
if (BuildConfig.DEBUG){
// 釋出release包時,移除log資訊
configuration.writeDebugLogs();
}
//Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(configuration.build());
}
}