開源框架利與弊 開源框架給開發者提供了便利,避免了重覆造輪子,但是卻隱藏了一些開發上的細節,如果不關註其內部實現,那麼將不利於開發人員掌握核心技術,當然也談不上更好的使用它,計劃分析項目的集成使用和低層實現。 基本四部曲 1. ImageLoaderConfiguration(有預設) 通過Imag ...
開源框架利與弊
開源框架給開發者提供了便利,避免了重覆造輪子,但是卻隱藏了一些開發上的細節,如果不關註其內部實現,那麼將不利於開發人員掌握核心技術,當然也談不上更好的使用它,計劃分析項目的集成使用和低層實現。
基本四部曲
- imageLoaderConfiguration
- ->imageLoader.init(imageLoaderConfiguration)
- displayImageOptions
- ->imageLoader.displayImage(...,displayImageOptions,...)
1. ImageLoaderConfiguration(有預設)
通過ImageLoaderConfiguration進行配置
兩種方式:
- 預設
ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this);
- 詳細的
File cacheDir = StorageUtils.getCacheDirectory(context); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800) // default = device screen dimensions .diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) .taskExecutor(...) .taskExecutorForCachedImages(...) .threadPoolSize(3) // default .threadPriority(Thread.NORM_PRIORITY - 1) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) // default .diskCache(new UnlimitedDiscCache(cacheDir)) // default .diskCacheSize(50 * 1024 * 1024) .diskCacheFileCount(100) .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default .imageDownloader(new BaseImageDownloader(context)) // default .imageDecoder(new BaseImageDecoder()) // default .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default .writeDebugLogs() .build();
2.給ImageLoader配置上一步信息
ImageLoader.getInstance().init(configuration);
ImageLoader使用單例模式:源碼
public static ImageLoader getInstance(){ /*為了線程安全加了鎖 *一但ImageLoader對象有了,就直接跳過同步 *這是單例設計的思想(可以參考HeadFirst設計模式) */ if(instance == null){ synchorinized(ImageLoader.class){ if(instance == null){ instance = new ImageLoader(); } } } return instance; }
public synchronized void init(){ if(configuration == null){ throw new IlleagalArgumentException(ERROR_INIT_CONFIG_WITH_NULL); if(this.configuration == null){ this.configuration = configuration; emptyListener = new SimpleIageLoadingListener(); fakeBitmapDisplayer = new FakeBitmapDisplayer(); } } }
3.下載圖片偏好
DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) // resource or drawable .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable .showImageOnFail(R.drawable.ic_error) // resource or drawable .resetViewBeforeLoading(false) // default .delayBeforeLoading(1000) .cacheInMemory(false) // default .cacheOnDisk(false) // default .preProcessor(...) .postProcessor(...) .extraForDownloader(...) .considerExifParams(false) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default .decodingOptions(...) .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build();
4.正式載入圖片
兩種方式:loadImage();displayImage()-一般用這個;
final ImageView mImageView = (ImageView) findViewById(R.id.image); String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg"; ImageSize mImageSize = new ImageSize(100, 100); //顯示圖片的配置 DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .bitmapConfig(Bitmap.Config.RGB_565) .build(); ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options, new SimpleImageLoadingListener(){ @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { super.onLoadingComplete(imageUri, view, loadedImage); mImageView.setImageBitmap(loadedImage); } });
GirdView,ListView中使用
使用GridView,ListView來顯示大量的圖片,而當我們快速滑動GridView,ListView,我們希望能停止圖片的載入,而在GridView,ListView停止滑動的時候載入當前界面的圖片,這個框架當然也提供這個功能,使用起來也很簡單,它提供了PauseOnScrollListener這個類來控制ListView,GridView滑動過程中停止去載入圖片
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
參考
- https://github.com/nostra13/Android-Universal-Image-Loader
- http://blog.csdn.net/xiaanming/article/details/26810303