安卓圖片框架:universal-image-loader的快速使用

來源:互聯網
上載者:User

安卓圖片框架: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,只要發送一張人臉照片,就可以進行識別並回複出各種資訊哦!它還有許多網頁小遊戲哦!還有更多精彩功能正在開發中! 快快關注吧!^ ^


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.