国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 武邑县| 金寨县| 丹东市| 四子王旗| 汉沽区| 凤台县| 鄱阳县| 建平县| 桑日县| 高青县| 巴彦县| 县级市| 扬中市| 锦屏县| 永宁县| 榆社县| 额济纳旗| 库车县| 奇台县| 随州市| 漳州市| 青河县| 乌兰浩特市| 贵州省| 海城市| 中方县| 延边| 鄂托克旗| 扶绥县| 仙居县| 文水县| 平阴县| 慈利县| 鹿泉市| 离岛区| 吉隆县| 乌恰县| 霍城县| 民丰县| 香河县| 诸暨市|