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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Image-Loader簡單使用

2019-11-09 17:43:00
字體:
供稿:網(wǎng)友

一、Inage-Loader特性: 1、支持多線程下載圖片,圖片可以來源于網(wǎng)絡(luò),文件系統(tǒng),項目文件夾assets中以及drawable中等 2、支持自定義的配置,例如線程池,圖片下載器,內(nèi)存緩存策略,硬盤緩存策略,圖片顯示選項等等 3、支持圖片的內(nèi)存緩存,文件系統(tǒng)緩存或者SD卡緩存 4、支持圖片下載過程的監(jiān)聽 5、根據(jù)控件(ImageView)的大小對Bitmap進行裁剪,減少Bitmap占用過多的內(nèi)存 6、較好的控制圖片的加載過程,例如暫停圖片加載,重新開始加載圖片,一般使用在ListView,GridView中,滑動過程中暫停加載圖片,停止滑動的時候去加載圖片 7、提供在較慢的網(wǎng)絡(luò)下對圖片進行加載 二、使用方法: 1、創(chuàng)建ImageLoader,所有的操作都由ImageLoader控制。該類使用單例設(shè)計模式,所以如果要獲取該類的實例,需要調(diào)用getInstance()方法。在使用ImageLoader顯示圖片之前,你首先要初始化它的配置,調(diào)用ImageLoaderConfiguration的init()方法,ok了。

//創(chuàng)建ImageLoader ImageLoaderConfiguration config = ImageLoaderConfiguration .createDefault(mContext); ImageLoader.getInstance().init(config);// 全局初始化此配置

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

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);

3、創(chuàng)建ImageLoader

ImageLoader imageLoader imageLoader = ImageLoader.getInstance();

4、緩存及圖像設(shè)置

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();

5.1、監(jiān)聽

imageLoader.loadImage(imageUrl, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String s, View view) { } @Override public void onLoadingFailed(String s, View view, FailReason failReason) { } @Override public void onLoadingComplete(String s, View view, Bitmap bitmap) { } @Override public void onLoadingCancelled(String s, View view) { } });

5.2、添加圖片

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

6、清理緩存:

imageLoader.clearMemoryCache(); imageLoader.clearDiskCache();
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长宁区| 驻马店市| 青田县| 调兵山市| 伊金霍洛旗| 桐城市| 宁城县| 盈江县| 扎赉特旗| 沐川县| 台中县| 彰化市| 榆林市| 彩票| 沙坪坝区| 马关县| 长岭县| 高阳县| 织金县| 资溪县| 廊坊市| 长白| 岐山县| 绩溪县| 荔波县| 林甸县| 武宣县| 曲沃县| 东港市| 潍坊市| 桦甸市| 中阳县| 东城区| 巴青县| 德庆县| 德保县| 丰台区| 霍城县| 宜阳县| 房山区| 西和县|