天天看點

第三方開源庫:imageLoader的使用

效果圖:

第三方開源庫:imageLoader的使用

注意:若app中需要使用圓角圖檔,且圖檔是根據url擷取的,可以使用imageLoader,隻需在參數設定中:displayer(new RoundedBitmapDisplayer(int rounded))

imageLoader使用步驟:

1 初始化

2 設定參數

3 display()

1 初始化:在application的onCreate()方法中初始化

public class MyApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		// imageLoader要在application的oncreate()方法中初始化
		initImageLoader();
	}
	
	public void initImageLoader() {
		//imageLoader要在application的oncreate()方法中初始化
		//1 設定參數
		ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
		
		config.threadPriority(Thread.NORM_PRIORITY - 2);
		config.denyCacheImageMultipleSizesInMemory();//不會在記憶體中緩存多個大小的圖檔
		config.diskCacheFileNameGenerator(new Md5FileNameGenerator());//為了保證圖檔名稱唯一
		config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
		//記憶體緩存大小預設是:app可用記憶體的1/8
		config.tasksProcessingOrder(QueueProcessingType.LIFO);
		config.writeDebugLogs(); // Remove for release app
		
		//2初始化
		ImageLoader.getInstance().init(config.build());
	}
}
           

2 設定參數:DisplayImageOptions

public class ImageLoaderOptions {
	// 1 listView中圖檔參數
	public static DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)// 加載中顯示的圖檔
			.showImageForEmptyUri(R.drawable.ic_default)// 圖檔url為空顯示的圖檔
			.showImageOnFail(R.drawable.ic_default)// 顯示失敗替代的圖檔
			.cacheInMemory(true)// 在記憶體中緩存
			.cacheOnDisk(true)// 在硬碟中緩存
			.considerExifParams(true)// 識别圖檔的方向資訊
			// .displayer(new FadeInBitmapDisplayer(3000))//圖檔漸變效果顯示
			.displayer(new RoundedBitmapDisplayer(1000)).build();// 圖檔圓角顯示:邊角處內圓的半徑值。
									// 若想圓形顯示,則設定>=imageview寬/2,且寬=高。[可直接給1000]
                                                                 //  RoundedBitmapDisplayer 當imageview的height= wrap_content高可能顯示不出來
 // 2 viewPager中參數設定()
	public static DisplayImageOptions pagerOptions = new DisplayImageOptions.Builder()

			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.resetViewBeforeLoading(true)// 加載前清空imageview上的圖檔
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.imageScaleType(ImageScaleType.EXACTLY)// 圖檔的縮放類型,對圖檔進行進一步的壓縮,EXACTLY_STRETCHED:拉伸
			.bitmapConfig(Config.RGB_565)// 設定圖檔的色彩模式,是比較節省記憶體的模式
			.displayer(new FadeInBitmapDisplayer(2000))
//			.displayer(new RoundedBitmapDisplayer(1000)) //當imageview的height=wrap_content高可能顯示不出來。
			.build();
}
           

3 調用display()方法

ImageLoader.getInstance().displayImage(Contant.urls[position], holder.imageView, ImageLoaderOptions.options);
           

源碼執行個體: http://download.csdn.net/detail/ss1168805219/9501847

Android Studio 中如何引用?

dependencies {
    ...
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    ...
}
           

繼續閱讀