/** * ImageLoaderConfiguration 建立的兩種方式。 */ // 建立默認的ImageLoaderConfiguration ImageLoaderConfiguration configuration_0 = ImageLoaderConfiguration .createDefault(this); // 使用DisplayImageOptions.Builder()建立DisplayImageOptions ImageLoaderConfiguration configuration_1 = new ImageLoaderConfiguration.Builder( this).threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() .build();
/** *當同一個Uri獲取不一樣大小的圖片,緩存到內存時,只緩存一個。默認會緩存多個不一樣的大小的相同圖片 */ denyCacheImageMultipleSizesInMemory()
/** * 設置本地圖片緩存 * @param discCache */ discCache(DiscCacheAware discCache)
DiscCacheAware 類型(在com.nostra13.universalimageloader.cache.disc.impl包下能找到以下的類): FileCountLimitedDiscCache(File cacheDir, int maxFileCount):設置緩存路徑和緩存文件的數量,超過數量後,old將被刪除 FileCountLimitedDiscCache(File cacheDir,FileNameGenerator fileNameGenerator,int maxFileCount):第二個參數是經過圖片的url生成的惟一文件名。 LimitedAgeDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, long maxAge) :第二個參數同上 LimitedAgeDiscCache(File cacheDir, long maxAge):maxAge爲定義的時間,超過期間後,圖片將被刪除 TotalSizeLimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, int maxCacheSize) :第二個參數同上 TotalSizeLimitedDiscCache(File cacheDir, int maxCacheSize) :定義緩存的大小,如超過了,就會刪除old圖片。 UnlimitedDiscCache(File cacheDir) :緩存沒有限制 UnlimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator):第二個參數同上
/** * 設置圖片保存到本地的參數 * @param maxImageWidthForDiscCache 保存的最大寬度 * @param maxImageHeightForDiscCache 保存的最大高度 * @param compressFormat 保存的壓縮格式 * @param compressQuality 提示壓縮的程度,有0-100.想png這種圖片無損耗,就沒必要設置了 */ discCacheExtraOptions(int maxImageWidthForDiscCache, int maxImageHeightForDiscCache, android.graphics.Bitmap.CompressFormat compressFormat, int compressQuality)
/** * 設置緩存文件的數量 * @param maxFileCount 數量 */ discCacheFileCount(int maxFileCount)
/** * 設置緩存的大小 * @param maxCacheSize 大小 */ discCacheSize(int maxCacheSize)
/** * 設置緩存文件的名字 * @param fileNameGenerator */ discCacheFileNameGenerator(FileNameGenerator fileNameGenerator) fileNameGenerator: HashCodeFileNameGenerator() :經過HashCode將url生成文件的惟一名字 Md5FileNameGenerator():經過Md5將url生產文件的惟一名字
/** * 啓動Log信息記錄,用於查看異常信息 */ enableLogging()
/** * 設置緩存信息 * @param maxImageWidthForMemoryCache 緩存圖片的最大寬度,默認爲手機的屏幕寬度 * @param maxImageHeightForMemoryCache 緩存圖片的最大高度,默認爲手機的屏幕寬度 */ memoryCacheExtraOptions(int maxImageWidthForMemoryCache, int maxImageHeightForMemoryCache)
/** * 添加個線程池,進行下載 * @param executor 線程池 * 若是進行了這個設置,那麼threadPoolSize(int),threadPriority(int),tasksProcessingOrder(QueueProcessingType) * 將不會起做用 */ taskExecutor(Executor executor)
/** * 設置用於顯示圖片的線程池大小 * @param threadPoolSize */ threadPoolSize(int threadPoolSize)
/** * 設置線程的優先級 * @param threadPriority */ threadPriority(int threadPriority)
/** * 設置圖片下載和顯示的工做隊列排序 * @param tasksProcessingType */ tasksProcessingOrder(QueueProcessingType tasksProcessingType)
/** * 下載緩存圖片 * @param executorForCachedImages */ taskExecutorForCachedImages(Executor executorForCachedImages)
/** * DisplayImageOptions 建立的兩種方式。 */ // 建立默認的DisplayImageOptions DisplayImageOptions option_0 = DisplayImageOptions.createSimple(); // 使用DisplayImageOptions.Builder()建立DisplayImageOptions DisplayImageOptions option_1 = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_launcher) .showImageOnFail(R.drawable.ic_error) .showImageForEmptyUri(R.drawable.ic_empty).cacheInMemory() .cacheOnDisc().displayer(new RoundedBitmapDisplayer(20)) .build();
//設置圖片在下載期間顯示的圖片 showStubImage(R.drawable.ic_launcher) //設置圖片Uri爲空或是錯誤的時候顯示的圖片 showImageForEmptyUri(R.drawable.ic_empty) //設置圖片加載/解碼過程當中錯誤時候顯示的圖片 showImageOnFail(R.drawable.ic_error) //設置圖片在下載前是否重置,復位 resetViewBeforeLoading() //設置下載的圖片是否緩存在內存中 cacheInMemory() //設置下載的圖片是否緩存在SD卡中 cacheOnDisc() //設置圖片的解碼類型 bitmapConfig(Bitmap.Config.RGB_565) //設置圖片的解碼配置 decodingOptions(android.graphics.BitmapFactory.Options decodingOptions) //設置圖片下載前的延遲 delayBeforeLoading(int delayInMillis) //設置額外的內容給ImageDownloader extraForDownloader(Object extra) //設置圖片加入緩存前,對bitmap進行設置 preProcessor(BitmapProcessor preProcessor) //設置顯示前的圖片,顯示後這個圖片一直保留在緩存中 postProcessor(BitmapProcessor postProcessor) //設置圖片以如何的編碼方式顯示 imageScaleType(ImageScaleType imageScaleType)
/** * 設置圖片的顯示方式 * @param displayer */ displayer(BitmapDisplayer displayer) displayer: RoundedBitmapDisplayer(int roundPixels)設置圓角圖片 FakeBitmapDisplayer()這個類什麼都沒作 FadeInBitmapDisplayer(int durationMillis)設置圖片漸顯的時間 SimpleBitmapDisplayer()正常顯示一張圖片
/** * 圖片的縮放方式 * @param imageScaleType */ imageScaleType(ImageScaleType imageScaleType) imageScaleType: EXACTLY :圖像將徹底按比例縮小的目標大小 EXACTLY_STRETCHED:圖片會縮放到目標大小徹底 IN_SAMPLE_INT:圖像將被二次採樣的整數倍 IN_SAMPLE_POWER_OF_2:圖片將下降2倍,直到下一減小步驟,使圖像更小的目標大小 NONE:圖片不會調整
package com.ryantang.rtimageloader;import android.app.Application;import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;/** * 初始化ImageLoaderConfiguration * * @author hsx * @time 2013-12-5下午05:38:43 */public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // This configuration tuning is custom. You can tune every option, you may tune some of them, // or you can create default configuration by // ImageLoaderConfiguration.createDefault(this); // method. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .threadPriority(Thread.NORM_PRIORITY - 2)//設置線程的優先級 .denyCacheImageMultipleSizesInMemory()//當同一個Uri獲取不一樣大小的圖片,緩存到內存時,只緩存一個。默認會緩存多個不一樣的大小的相同圖片 .discCacheFileNameGenerator(new Md5FileNameGenerator())//設置緩存文件的名字 .discCacheFileCount(60)//緩存文件的最大個數 .tasksProcessingOrder(QueueProcessingType.LIFO)// 設置圖片下載和顯示的工做隊列排序 .enableLogging() //是否打印日誌用於檢查錯誤 .build(); //Initialize ImageLoader with configuration ImageLoader.getInstance().init(config); } }
package com.ryantang.rtimageloader.listener;import java.util.Collections;import java.util.LinkedList;import java.util.List;import android.graphics.Bitmap;import android.view.View;import android.widget.ImageView;import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;public class AnimateFirstDisplayListener extends SimpleImageLoadingListener { public static List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { FadeInBitmapDisplayer.animate(imageView, 500); displayedImages.add(imageUri); } } } }
package com.ryantang.rtimageloader.adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;import com.ryantang.rtimageloader.R;import com.ryantang.rtimageloader.listener.AnimateFirstDisplayListener;public class ItemAdapter extends BaseAdapter { DisplayImageOptions options; private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); String[] imageUrls; Context context; public ItemAdapter(String[] imageUrls, Context context) { super(); this.imageUrls = imageUrls; this.context = context; options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_launcher)//設置圖片在下載期間顯示的圖片 .showImageForEmptyUri(R.drawable.ic_launcher)//設置圖片Uri爲空或是錯誤的時候顯示的圖片 .showImageOnFail(R.drawable.ic_launcher)//設置圖片加載/解碼過程當中錯誤時候顯示的圖片 .cacheInMemory(true)//是否緩存都內存中 .cacheOnDisc(true)//是否緩存到sd卡上 .displayer(new RoundedBitmapDisplayer(20)) .build(); } private class ViewHolder { public TextView text; public ImageView image; } @Override public int getCount() { return imageUrls.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.item_list_image, parent, false); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText("Item " + (position + 1)); // ImageLoader ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener); return convertView; } }