Android-Universal-Image-Loader是一個開源的UI組件程序,該項目的目的是提供一個可重複使用的儀器爲異步圖像加載,緩存和顯示。android
(1).使用多線程加載圖片
(2).靈活配置ImageLoader的基本參數,包括線程數、緩存方式、圖片顯示選項等;
(3).圖片異步加載緩存機制,包括內存緩存及SDCard緩存;
(4).採用監聽器監聽圖片加載過程及相應事件的處理;
(5).配置加載的圖片顯示選項,好比圖片的圓角處理及漸變更畫。git
//第一種方法是去網上下載 github
地址:https://github.com/nostra13/Android-Universal-Image-Loader緩存
//第二種就是能夠依賴 網絡
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'多線程
1 配置文件要設置網絡權限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
先建一個App類 繼承Application (其中兩種加載)
建好後必需要在配置文件內初始化(註冊.App) 2
3 public class App extends Application { 4 @Override 5 public void onCreate() { 6 super.onCreate(); 7 8 //第一種是建立默認的加載 9 10 //建立默認的ImageLoaderConfiguration 11 ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this); 12 //初始化ImageLoader 13 ImageLoader.getInstance().init(configuration); 14 15 //第二種是自定義加載 16 //自定義加載第三方圖片樣式 17 DisplayImageOptions options = new DisplayImageOptions.Builder() 18 .showImageOnLoading(R.drawable.image_nor) // 設置圖片下載期間顯示的圖片 19 .showImageForEmptyUri(R.drawable.image_nor) // 設置圖片Uri爲空或是錯誤的時候顯示的圖片 20 .showImageOnFail(R.drawable.image_nor) // 設置圖片加載或解碼過程當中發生錯誤顯示的圖片 21 .resetViewBeforeLoading(true) // default 設置圖片在加載前是否重置、復位 22 .delayBeforeLoading(200) // 下載前的延遲時間 23 .cacheInMemory(true) // default 設置下載的圖片是否緩存在內存中 24 .cacheOnDisk(true) // default 設置下載的圖片是否緩存在SD卡中 25 .considerExifParams(false) // default 26 .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 設置圖片以如何的編碼方式顯示 27 .bitmapConfig(Bitmap.Config.RGB_565) // default 設置圖片的解碼類型 28 // .displayer(new SimpleBitmapDisplayer()) // default 還能夠設置圓角圖片new RoundedBitmapDisplayer(20) 29 .displayer(new FadeInBitmapDisplayer(200))// 圖片加載好後漸入的動畫時間 30 // .displayer(new RoundedBitmapDisplayer(20)) // 設置成圓角圖片 31 .handler(new Handler()) // default 32 .build(); 33 34 //設置圖片緩存路徑 35 File cacheDir = StorageUtils.getCacheDirectory(this); 36 //自定義ImageLoaderConfiguration配置 37 ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this) 38 .memoryCacheExtraOptions(480, 800) // default = device screen dimensions 內存緩存文件的最大寬、高 39 // .diskCacheExtraOptions(480, 800, null) // 本地緩存的詳細信息(緩存的最大寬、高),最好不要設置這個 40 .threadPoolSize(4) // default 線程池內加載的數量 41 // .threadPriority(Thread.NORM_PRIORITY - 2) // default 設置當前線程的優先級 42 .tasksProcessingOrder(QueueProcessingType.FIFO) // default 43 .denyCacheImageMultipleSizesInMemory() 44 .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //能夠經過本身的內存緩存實現 45 .memoryCacheSize(2 * 1024 * 1024) // 內存緩存的最大值 46 .memoryCacheSizePercentage(13) // default 47 //能夠自定義緩存路徑 48 .diskCache(new UnlimitedDiskCache(cacheDir)) 49 .diskCacheSize(100 * 1024 * 1024) // 100 Mb sd卡(本地)緩存的最大值 50 // .diskCacheFileCount(100) // 能夠緩存的文件數量 51 // default爲使用HASHCODE對UIL進行加密命名, 還能夠用MD5(new Md5FileNameGenerator())加密 52 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) 53 // .connectTimeout (5 s), readTimeout (30 s) //超時時間 54 //第二個參數:鏈接超時時間,第三個參數:讀取超時時間 55 .imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) 56 .defaultDisplayImageOptions(options) 57 .writeDebugLogs() // 打印debug log 58 .build(); //開始構建 59 60 //初始化ImageLoader 61 ImageLoader.getInstance().init(configuration); 62 63 }
1 在activity應用 2 3 public class MainActivity extends AppCompatActivity { 4 5 private String url; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 //初始化組件 13 GridView gridView= (GridView) findViewById(R.id.lv); 14 gridView.setAdapter(new ImageAdapter(this,ImageUrlArrays.IMAGE_URL_ARRAYS)); 15 16 17 /* //初始化圖片加載框架 18 final ImageLoader imageLoader = ImageLoader.getInstance();*/ 19 20 //第一種加載方式 須要個url圖片地址 21 url = "http://www.chinagirlol.cc/data/attachment/forum/201412/03/233758hw7o7h08kkozkcwi.jpg"; 22 imageLoader.displayImage(ImageUrlArrays.IMAGE_URL_ARRAYS[8],imageView); 23 24 /* //第二種加載方式 25 imageLoader.displayImage(url, imageView, new ImageLoadingListener() { 26 @Override //剛開始加載的圖片 27 public void onLoadingStarted(String s, View view) { 28 imageView.setImageResource(R.drawable.image_nor); 29 } 30 31 @Override //加載失敗圖片 32 public void onLoadingFailed(String s, View view, FailReason failReason) { 33 34 } 35 36 @Override//加載完成的圖片 37 public void onLoadingComplete(String s, View view, Bitmap bitmap) { 38 imageView.setImageBitmap(bitmap); 39 } 40 41 @Override//取消加載 42 public void onLoadingCancelled(String s, View view) { 43 44 } 45 });*/ 46 47 /* //第三種加載方式 設固定寬高 48 ImageSize imageSize=new ImageSize(140,160); 49 imageLoader.loadImage(ImageUrlArrays.IMAGE_URL_ARRAYS[10],imageSize,new SimpleImageLoadingListener(){ 50 @Override 51 public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 52 super.onLoadingComplete(imageUri, view, loadedImage); 53 imageView.setImageBitmap(loadedImage); 54 } 55 }); 56 */ 57 } 58 }
以上代碼在之後的修改比較麻煩 因此咱們能夠本身在封裝起來,變成本身的再用,方便修改框架
1 public class WXHLImageLoader { 2 3 //懶漢單例 4 private static WXHLImageLoader wxhlImageLoader; 5 //獲取第三方的ImageLoader 6 private ImageLoader imageLoader; 7 8 private WXHLImageLoader(){ 9 imageLoader = ImageLoader.getInstance(); 10 } 11 12 /** 13 * 單例 14 * @return 15 */ 16 public static synchronized WXHLImageLoader getInstance(){ 17 // return wxhlImageLoader; 18 if(wxhlImageLoader == null){ 19 wxhlImageLoader = new WXHLImageLoader(); 20 } 21 return wxhlImageLoader; 22 } 23 24 /** 25 * 顯示圖片 26 * @param imageUrl 27 * @param imageView 28 */ 29 public void displayImage(String imageUrl, ImageView imageView){ 30 imageLoader.displayImage(imageUrl,imageView); 31 } 32 33 /** 34 * 顯示圖片,帶監聽器的 35 * @param imageUrl 36 * @param imageView 37 */ 38 public void displayImage(String imageUrl, ImageView imageView,ImageLoadingListener listener){ 39 imageLoader.displayImage(imageUrl,imageView,listener); 40 } 41 42 /** 43 * 加載圖片,帶監聽器的 44 * @param imageUrl 45 * @param listener 46 */ 47 public void loadImage(String imageUrl, SimpleImageLoadingListener listener){ 48 imageLoader.loadImage(imageUrl,listener); 49 } 50 51 /** 52 * 加載圖片,帶監聽器的 53 * @param imageUrl 54 * @param listener 55 */ 56 public void loadImage(String imageUrl, int width, int height, SimpleImageLoadingListener listener){ 57 ImageSize targetSize = new ImageSize(width, height); // result Bitmap will be fit to this size 58 imageLoader.loadImage(imageUrl,targetSize,listener); 59 } 60 61 62 }