本篇文章為大家展示了Android中怎么配置 Imageloader,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶提供專業(yè)的服務(wù)器托管,四川各地服務(wù)器托管,服務(wù)器托管、多線服務(wù)器托管.托管咨詢專線:18982081108
ImageLoader 優(yōu)點
(1) 支持下載進度監(jiān)聽
(2) 可以在 View 滾動中暫停圖片加載
通過 PauseOnScrollListener 接口可以在 View 滾動中暫停圖片加載。
(3) 默認實現(xiàn)多種內(nèi)存緩存算法 這幾個圖片緩存都可以配置緩存算法,不過 ImageLoader 默認實現(xiàn)了較多緩存算法,如 Size
最大先刪除、使用最少先刪除、最近最少使用、先進先刪除、時間最長先刪除等。
(4) 支持本地緩存文件名規(guī)則定義
實現(xiàn)代碼:
/** * 初始化ImageLoader */ public static void initImageLoader(Context context) { File cacheDir = StorageUtils.getOwnCacheDirectory(context, "bee_k77/Cache");// 獲取到緩存的目錄地址 Log.e("cacheDir", cacheDir.getPath()); // 創(chuàng)建配置ImageLoader(所有的選項都是可選的,只使用那些你真的想定制),這個可以設(shè)定在APPLACATION里面,設(shè)置為全局的配置參數(shù) ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( context) // max width, max height,即保存的每個緩存文件的最大長寬 .memoryCacheExtraOptions(480, 800) // Can slow ImageLoader, use it carefully (Better don't use it)設(shè)置緩存的詳細信息,最好不要設(shè)置這個 / .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) // 線程池內(nèi)加載的數(shù)量 .threadPoolSize(3) // 線程優(yōu)先級 .threadPriority(Thread.NORM_PRIORITY - 2) /* * When you display an image in a small ImageView * and later you try to display this image (from identical URI) in a larger ImageView * so decoded image of bigger size will be cached in memory as a previous decoded image of smaller size. * So the default behavior is to allow to cache multiple sizes of one image in memory. * You can deny it by calling this method: * so when some image will be cached in memory then previous cached size of this image (if it exists) * will be removed from memory cache before. */ / .denyCacheImageMultipleSizesInMemory() // You can pass your own memory cache implementation你可以通過自己的內(nèi)存緩存實現(xiàn) // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // .memoryCacheSize(2 * 1024 * 1024) //硬盤緩存50MB .diskCacheSize(50 * 1024 * 1024) //將保存的時候的URI名稱用MD5 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) // 加密 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//將保存的時候的URI名稱用HASHCODE加密 .tasksProcessingOrder(QueueProcessingType.LIFO) .diskCacheFileCount(100) //緩存的File數(shù)量 .diskCache(new UnlimitedDiscCache(cacheDir))// 自定義緩存路徑 // .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // .imageDownloader(new BaseImageDownloader(context, 5 * 1000, // 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超時時間 .writeDebugLogs() // Remove for release app .build(); // Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config);// 全局初始化此配置 }
Option類
package com.topnews.config; import android.graphics.Bitmap; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.topnews.R; public class Options { /** * 新聞列表中用到的圖片加載配置 */ public static DisplayImageOptions getListOptions() { DisplayImageOptions options = new DisplayImageOptions.Builder() // 設(shè)置圖片在下載期間顯示的圖片 .showImageOnLoading(R.drawable.ic_stub) // 設(shè)置圖片Uri為空或是錯誤的時候顯示的圖片 .showImageForEmptyUri(R.drawable.ic_stub) // 設(shè)置圖片加載/解碼過程中錯誤時候顯示的圖片 .showImageOnFail(R.drawable.ic_error) // 設(shè)置下載的圖片是否緩存在內(nèi)存中 .cacheInMemory(false) // 設(shè)置下載的圖片是否緩存在SD卡中 .cacheOnDisc(true) // 保留Exif信息 .considerExifParams(true) // 設(shè)置圖片以如何的編碼方式顯示 .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) // 設(shè)置圖片的解碼類型 .bitmapConfig(Bitmap.Config.RGB_565) // .decodingOptions(android.graphics.BitmapFactory.Options // decodingOptions)//設(shè)置圖片的解碼配置 .considerExifParams(true) // 設(shè)置圖片下載前的延遲 .delayBeforeLoading(100)// int // delayInMillis為你設(shè)置的延遲時間 // 設(shè)置圖片加入緩存前,對bitmap進行設(shè)置 // .preProcessor(BitmapProcessor preProcessor) .resetViewBeforeLoading(true)// 設(shè)置圖片在下載前是否重置,復位 // .displayer(new RoundedBitmapDisplayer(20))//是否設(shè)置為圓角,弧度為多少 .displayer(new FadeInBitmapDisplayer(100))// 淡入 .build(); return options; } }
上述內(nèi)容就是Android中怎么配置 Imageloader,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:Android中怎么配置Imageloader
鏈接URL:http://m.newbst.com/article4/jechie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、品牌網(wǎng)站制作、網(wǎng)站排名、面包屑導航、網(wǎng)站內(nèi)鏈、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)