android imageloader 路径,Android中的Universal-Image-Loader的使用
大家在做Android開發時,經常需要用到異步加載圖片,在這里主要介紹最常用的一個工具Universal-Image-Loader,相信很多朋友都聽過或者使用過這個強大的圖片加載框架。有關Universal-Image-Loader的使用可以參考 https://github.com/nostra13/Android-Universal-Image-Loader,看一看到主要有以下特征:
Features
Multithread image loading (async or sync)
Wide customization of ImageLoader's configuration (thread executors, downloader, decoder, memory and disk cache, display image options, etc.)
Many customization options for every display image call (stub images, caching switch, decoding options, Bitmap processing and displaying, etc.)
Image caching in memory and/or on disk (device's file system or SD card)
Listening loading process (including downloading progress)
Android 2.0+ support
接下來看一下Universal-Image-Loader開源庫的使用。在這里要說明一下我用到的開發工具是Android Studio。
1.創建一個Android工程目錄,并把Universal-Image-Loader放在libs文件夾下。
20151231102826331.jpg
2.配置ImageLoaderConfiguration,這個是圖片加載器ImageLoader的配置參數。可以選擇在Application中配置,目的的每次用到的時候不用重復寫代碼。
Application和Activity,Service一樣是Android框架的一個系統組件,當Android程序啟動時系統會創建一個Application對象,用來存儲系統的一些信息。
Android系統自動會為每個程序運行時創建一個Application類的對象且只創建一個,所以Application可以說是單例(singleton)模式的一個類。
通常我們是不需要指定一個Application的,系統會自動幫我們創建,如果需要創建自己的Application,那也很簡單!創建一個類繼承Application并在AndroidManifest.xml文件中的application標簽中進行注冊(只需要給application標簽增加name屬性,并添加自己的 Application的名字即可)。
啟動Application時,系統會創建一個PID,即進程ID,所有的Activity都會在此進程上運行。那么我們在Application創建的時候初始化全局變量,同一個應用的所有Activity都可以取到這些全局變量的值,換句話說,我們在某一個Activity中改變了這些全局變量的值,那么在同一個應用的其他Activity中值就會改變。
Application對象的生命周期是整個程序中最長的,它的生命周期就等于這個程序的生命周期。因為它是全局的單例的,所以在不同的Activity,Service中獲得的對象都是同一個對象。所以可以通過Application來進行一些,如:數據傳遞、數據共享和數據緩存等操作。
注:繼承Application類,主要重寫里面的onCreate()方法(android.app.Application包的onCreate()才是真正的Android程序的入口點),就是創建的時候,初始化變量的值。然后在整個應用中的各個文件中就可以對該變量進行操作了。
新建一個MyApplication繼承Application,并在onCreate()中創建ImageLoader的配置參數
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader(getApplicationContext());
}
private void initImageLoader(Context context){
/**定義緩存文件的目錄**/
File cacheDir= StorageUtils.getOwnCacheDirectory(getApplicationContext(),"Cache/");
/**ImageLoader的配置**/
ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY-2) //設置同時運行的線程
.denyCacheImageMultipleSizesInMemory() //緩存顯示不同大小的同一張圖片
.diskCacheSize(50*1024*1024) //50MB SD卡本地緩存的最大值
.diskCache(new UnlimitedDiscCache(cacheDir)) //SD卡緩存
.memoryCache(new WeakMemoryCache()) //內存緩存
.tasksProcessingOrder(QueueProcessingType.LIFO).build();
//全局初始化配置
ImageLoader.getInstance().init(config);
}
} ```
后面附上網上比較全的配置信息,實際上不用每一個都用到。
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build(); ```
不要忘了在AndroidManifest中注冊MyApplication,加入權限。
3. ImageLoader加載圖片
使用ImageLoader加載圖片時,首先要實例化ImageLoader。ImageLoader的實例化采用的是單例模式。
ImageLoader loader=ImageLoader.getInstance();
ImageLoader加載圖片分為兩種情況,一種是默認的加載。
loader.displayImage(imgUrl,img);//參數1--要加載圖片的url地址,參數2--要顯示圖片的控件,可以為ImageView
另一種是通過DisplayImageOptions 自定義要顯示的圖片。
加載圖片的方法是:
loader.displayImage(imgUrl,img,options);
options在下面定義
DisplayImageOptions options=new DisplayImageOptions.Builder()
.showImageOnLoading(drawableID) //設置正在下載時顯示的圖片
.showImageForEmptyUri(drawableID) //設置Url為空時顯示的圖片
.showImageOnFail(drawableID) //設置圖片下載失敗時顯示的圖片
.cacheInMemory(true) //允許圖片保存到手機內存
.cacheOnDisk(true) //允許圖片保存到SD卡中
.considerExifParams(true) //是否考慮JPEG圖像EXIF參數(旋轉,翻轉)
.bitmapConfig(Bitmap.Config.RGB_565)
.build(); //構建完成 ```
附上網上比較全的配置信息
DisplayImageOptions options;options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher) //設置圖片在下載期間顯示的圖片
.showImageForEmptyUri(R.drawable.ic_launcher)//設置圖片Uri為空或是錯誤的時候顯示的圖片
.showImageOnFail(R.drawable.ic_launcher) //設置圖片加載/解碼過程中錯誤時候顯示的圖片
.cacheInMemory(true)//設置下載的圖片是否緩存在內存中
.cacheOnDisc(true)//設置下載的圖片是否緩存在SD卡中
.considerExifParams(true) //是否考慮JPEG圖像EXIF參數(旋轉,翻轉)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//設置圖片以如何的編碼方式顯示
.bitmapConfig(Bitmap.Config.RGB_565)//設置圖片的解碼類型//
.decodingOptions(android.graphics.BitmapFactory.Options decodingOptions)//設置圖片的解碼配置
//.delayBeforeLoading(int delayInMillis)//int delayInMillis為你設置的下載前的延遲時間
//設置圖片加入緩存前,對bitmap進行設置
//.preProcessor(BitmapProcessor preProcessor)
.resetViewBeforeLoading(true)//設置圖片在下載前是否重置,復位
.displayer(new RoundedBitmapDisplayer(20))//是否設置為圓角,弧度為多少
.displayer(new FadeInBitmapDisplayer(100))//是否圖片加載好后漸入的動畫時間
.build();//構建完成 ```
4.注意事項
上述提到的2個權限必須加入,否則會出錯
ImageLoaderConfiguration必須配置并且全局化的初始化這個配置ImageLoader.getInstance().init(config); 否則也會出現錯誤提示
ImageLoader是根據ImageView的height,width確定圖片的寬高。
如果經常出現OOM(別人那邊看到的,覺得很有提的必要)
①減少配置之中線程池的大小,(.threadPoolSize).推薦1-5;
②使用.bitmapConfig(Bitmap.config.RGB_565)代替ARGB_8888;
③使用.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者try.imageScaleType(ImageScaleType.EXACTLY);
④避免使用RoundedBitmapDisplayer.他會創建新的ARGB_8888格式的Bitmap對象;
⑤使用.memoryCache(new WeakMemoryCache()),不要使用.cacheInMemory();
5.從其他路徑加載圖片的url
多線程異步加載和顯示圖片(圖片來源于網絡、sd卡、assets文件夾,drawable文件夾(不能加載9patch),新增加載視頻縮略圖)
"file:///mnt/sdcard/image.png"// from SD card
"file:///mnt/sdcard/video.mp4"// from SD card (video thumbnail)
"content://media/external/video/media/13"// from content provider (video thumbnail)
"drawable://"+ R.drawable.img // from drawables (non-9patch images)
總結
以上是生活随笔為你收集整理的android imageloader 路径,Android中的Universal-Image-Loader的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue Resource
- 下一篇: Android花样Text设置神器之Sp