安卓圖片框架:universal-image-loader的快速使用
在安卓開發過程中難免會遇到以下幾個情況:
1、圖片非同步載入
2、圖片緩衝
3、圖片顯示
4、其它……(忘記了)
以上的這些情況,可能要自己去寫不少代碼去實現這些功能,而且對於一些新手,可能寫了半天,發現效果很不理想、記憶體問題也處理不好、非同步也不好控制(我就是這樣)。所以搜了一下,發現網上最流行的一個安卓圖片開源架構 universal-image-loader,親自使用了一下,發現真是的好用啊!一共就加十幾行代碼就搞定了,媽媽再也不用擔心我花時間去瞎搞八搞的了!
好了,下面來說說快速使用這個架構的步驟:
一、往工程的libs檔案夾中添加 universal-image-loader-1.9.2-with-sources.jar 這個Jar包。點擊這裡可以直接下載哦!
二、建立一個 繼承application的類,其中的代碼看下面:
public class MyApplication extends Application {public void onCreate() {super.onCreate();initImageLoader(getApplicationContext());}public static void initImageLoader(Context context) {ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.MAX_PRIORITY).denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove for release app.build();ImageLoader.getInstance().init(config);}}
注意,這個application類要在menifest中的application標籤中註明哦!就是下面這樣:
<>
android:name=com.lee.example.MyApplication
好了,添加完以上兩步的內容,基本的架構環境就搭建好了,下面是如何使用它來載入網狀圖片:
先聲明兩個對象:
private DisplayImageOptions options;
private ImageLoader imageLoader;
然後在類的建構函式或者activity的increate方法中初始化它們:
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(null)//載入過程中顯示的圖片
.showImageForEmptyUri(null)//載入內容為空白顯示的圖片
.showImageOnFail(null)//載入失敗顯示的圖片
.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(388)).build();
下面是使用:
imageLoader.displayImage(imageFile, imageView, options);
說明:
imageFile:圖片的網路路徑(也可以用本地的路徑哦,詳情看文末的架構github中文檔說明)
imageView:就是圖片控制項哈~
就上面這麼一句,太方便了哈!
小廣告:關注公眾號:“大大花貓” ,它是一個智能聊天機器人,可以回答各種問題,包括查詢各種資訊哦!它還可以進行Face Service,只要發送一張人臉照片,就可以進行識別並回複出各種資訊哦!它還有許多網頁小遊戲哦!還有更多精彩功能正在開發中! 快快關注吧!^ ^