国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Universal ImageLoader 緩存圖片

2020-04-11 10:59:47
字體:
供稿:網(wǎng)友

項目介紹:

Android上最讓人頭疼的莫過于從網(wǎng)絡(luò)獲取圖片、顯示、回收,任何一個環(huán)節(jié)有問題都可能直接OOM,這個項目或許能幫到你。Universal Image Loader for Android的目的是為了實現(xiàn)異步的網(wǎng)絡(luò)圖片加載、緩存及顯示,支持多線程異步加載。它最初來源于Fedor Vlasov的項目,且自此之后,經(jīng)過大規(guī)模的重構(gòu)和改進。

特性列舉:

多線程下載圖片,圖片可以來源于網(wǎng)絡(luò),文件系統(tǒng),項目文件夾assets中以及drawable中等
支持隨意的配置ImageLoader,例如線程池,圖片下載器,內(nèi)存緩存策略,硬盤緩存策略,圖片顯示選項以及其他的一些配置
支持圖片的內(nèi)存緩存,文件系統(tǒng)緩存或者SD卡緩存
支持圖片下載過程的監(jiān)聽
根據(jù)控件(ImageView)的大小對Bitmap進行裁剪,減少Bitmap占用過多的內(nèi)存
較好的控制圖片的加載過程,例如暫停圖片加載,重新開始加載圖片,一般使用在ListView,GridView中,滑動過程中暫停加載圖片,停止滑動的時候去加載圖片
提供在較慢的網(wǎng)絡(luò)下對圖片進行加載

使用過程:

創(chuàng)建默認的ImageLoader,所有的操作都由ImageLoader控制。該類使用單例設(shè)計模式,所以如果要獲取該類的實力,需要調(diào)用getInstance()方法。在使用ImageLoader顯示圖片之前,你首先要初始化它的配置,調(diào)用ImageLoaderConfiguration的init()方法,然后你就可以實現(xiàn)各種的顯示了。

//創(chuàng)建默認的ImageLoader配置參數(shù) ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this); //Initialize ImageLoader with configuration. ImageLoader.getInstance().init(configuration); 

自定義配置imageloader, 就像你已經(jīng)知道的,首先,你需要使用ImageLoaderConfiguration對象來初始化ImageLoader。由于ImageLoader是單例,所以在程序開始的時候只需要初始化一次就好了。建議你在Activity的onCreate()方法中初始化。如果一個ImageLoader已經(jīng)初始化過,再次初始化不會有任何效果。下面我們通過ImageLoaderConfiguration.Builder創(chuàng)建一個設(shè)置

File cacheDir =StorageUtils.getOwnCacheDirectory(this, "imageloader/Cache"); ImageLoaderConfigurationconfig = new ImageLoaderConfiguration .Builder(this) .memoryCacheExtraOptions(480, 800) // maxwidth, max height,即保存的每個緩存文件的最大長寬 .threadPoolSize(3)//線程池內(nèi)加載的數(shù)量 .threadPriority(Thread.NORM_PRIORITY -2) .denyCacheImageMultipleSizesInMemory() .memoryCache(new UsingFreqLimitedMemoryCache(2* 1024 * 1024)) // You can pass your own memory cache implementation/你可以通過自己的內(nèi)存緩存實現(xiàn) .memoryCacheSize(2 * 1024 * 1024) .discCacheSize(50 * 1024 * 1024) .discCacheFileNameGenerator(newMd5FileNameGenerator())//將保存的時候的URI名稱用MD5 加密 .tasksProcessingOrder(QueueProcessingType.LIFO) .discCacheFileCount(100) //緩存的文件數(shù)量 .discCache(new UnlimitedDiscCache(cacheDir))//自定義緩存路徑 .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) .imageDownloader(new BaseImageDownloader(this,5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超時時間 .writeDebugLogs() // Remove for releaseapp .build();//開始構(gòu)建 ImageLoader.getInstance().init(config); 

得到imageLoader

ImageLoader imageLoader imageLoader = ImageLoader.getInstance(); 

使用過程:

(1)圖像操作是否參與緩存以及圖像效果的配置操作

DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) //加載圖片時的圖片 .showImageForEmptyUri(R.drawable.ic_empty) //沒有圖片資源時的默認圖片 .showImageOnFail(R.drawable.ic_error) //加載失敗時的圖片 .cacheInMemory(true) //啟用內(nèi)存緩存 .cacheOnDisk(true) //啟用外存緩存 .considerExifParams(true) //啟用EXIF和JPEG圖像格式 .displayer(new RoundedBitmapDisplayer(20)) //設(shè)置顯示風(fēng)格這里是圓角矩形 .build(); 

DisplayImageOptions以下是所有默認配置參數(shù)根據(jù)需求可以自定義配置

private int imageResOnLoading = 0; private int imageResForEmptyUri = 0; private int imageResOnFail = 0; private Drawable imageOnLoading = null; private Drawable imageForEmptyUri = null; private Drawable imageOnFail = null; private boolean resetViewBeforeLoading = false; private boolean cacheInMemory = false; private boolean cacheOnDisk = false; private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2; private Options decodingOptions = new Options(); private int delayBeforeLoading = 0; private boolean considerExifParams = false; private Object extraForDownloader = null; private BitmapProcessor preProcessor = null; private BitmapProcessor postProcessor = null; private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer(); private Handler handler = null; private boolean isSyncLoading = false; 

(2)圖片加載監(jiān)聽器在這里吧可以設(shè)置加載時的動畫或者進度條之類的東西這里

ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener { static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { FadeInBitmapDisplayer.animate(imageView, 500); displayedImages.add(imageUri); } } } }

(3)簡單設(shè)置就可以給ImageView添加圖片了

imageLoader.displayImage(imageUrl, imageview, options, animateFirstListener); 

對于本地的圖片 ,在其絕對地址前面要加入"file://"。網(wǎng)絡(luò)圖片就直接寫路徑了。

由于我的這個是最新的包,可能跟以前老的版本不同,看到有些網(wǎng)友說的是:

String imageUri = "http://site.com/image.png"; // 網(wǎng)絡(luò)圖片 String imageUri = "file:///mnt/sdcard/image.png"; //SD卡圖片 String imageUri = "content://media/external/audio/albumart/13"; // 媒體文件夾 String imageUri = "assets://image.png"; // assets String imageUri = "drawable://" + R.drawable.image; // drawable文件 

緩存的清理:

緩存的清理可以按需求來定,可以再每個Activity的生命周期函數(shù)onDestroy中清理也可以單獨設(shè)置讓用戶自行清理。

@Override public void onDestroy() { super.onDestroy(); imageLoader.clearMemoryCache(); imageLoader.clearDiskCache(); }

GirdView,ListView加載圖片:

相信大部分人都是使用GridView,ListView來顯示大量的圖片,而當(dāng)我們快速滑動GridView,ListView,我們希望能停止圖片的加載,而在GridView,ListView停止滑動的時候加載當(dāng)前界面的圖片,這個框架當(dāng)然也提供這個功能,使用起來也很簡單,它提供了PauseOnScrollListener這個類來控制ListView,GridView滑動過程中停止去加載圖片,該類使用的是代理模式

listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling)); gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling)); 

第一個參數(shù)就是我們的圖片加載對象ImageLoader, 第二個是控制是否在滑動過程中暫停加載圖片,如果需要暫停傳true就行了,第三個參數(shù)控制猛的滑動界面的時候圖片是否加載

OutOfMemoryError:

雖然這個框架有很好的緩存機制,有效的避免了OOM的產(chǎn)生,一般的情況下產(chǎn)生OOM的概率比較小,但是并不能保證OutOfMemoryError永遠不發(fā)生,這個框架對于OutOfMemoryError做了簡單的catch,保證我們的程序遇到OOM而不被crash掉,但是如果我們使用該框架經(jīng)常發(fā)生OOM,我們應(yīng)該怎么去改善呢?

減少線程池中線程的個數(shù),在ImageLoaderConfiguration中的(.threadPoolSize)中配置,推薦配置1-5

在DisplayImageOptions選項中配置bitmapConfig為Bitmap.Config.RGB_565,因為默認是ARGB_8888, 使用RGB_565會比使用ARGB_8888少消耗2倍的內(nèi)存

在ImageLoaderConfiguration中配置圖片的內(nèi)存緩存為memoryCache(newWeakMemoryCache()) 或者不使用內(nèi)存緩存

在DisplayImageOptions選項中設(shè)置.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY)

通過上面這些,相信大家對Universal-Image-Loader框架的使用已經(jīng)非常的了解了,我們在使用該框架的時候盡量的使用displayImage()方法去加載圖片,loadImage()是將圖片對象回調(diào)到ImageLoadingListener接口的onLoadingComplete()方法中,需要我們手動去設(shè)置到ImageView上面,displayImage()方法中,對ImageView對象使用的是Weak references,方便垃圾回收器回收ImageView對象,如果我們要加載固定大小的圖片的時候,使用loadImage()方法需要傳遞一個ImageSize對象,而displayImage()方法會根據(jù)ImageView對象的測量值,或者android:layout_width and android:layout_height設(shè)定的值,或者android:maxWidth and/or android:maxHeight設(shè)定的值來裁剪圖片

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 台东县| 虎林市| 承德市| 会泽县| 万年县| 怀来县| 永丰县| 镇平县| 明光市| 尼玛县| 宁安市| 渑池县| 永寿县| 汾西县| 化德县| 贡嘎县| 密云县| 克什克腾旗| 甘谷县| 广平县| 历史| 县级市| 铜川市| 封开县| 济宁市| 精河县| 新乐市| 南丰县| 遵化市| 正定县| 三都| 合肥市| 南木林县| 武义县| 沅江市| 安多县| 武强县| 湘潭市| 武乡县| 辽阳县| 唐河县|