標籤:
開篇——介紹Picasso
(Picasso是什嗎?)Picasso:A Powerfull Image Downloading and Caching Library for Android,即Android平台的網狀圖片下載和緩衝架構。
(Picasso如何使用?)架構嘛!既然牛人能夠寫出這個架構,自然使用流暢。不用擔心,很簡單,但深入原始碼就需要花點功夫。
(為什麼會出現Picasso架構?)Android開發中,常需要從遠程伺服器端擷取圖片;一般情況下,我們會使用HttpURLConnection(java.net.HttpURLConnection)和AsyncTask(android.os.AsyncTask)結合實現該功能;但還需要考慮其他情況:圖片緩衝和下載管理(??),因此聰明的做法是:封裝成自己的工具類,或者是使用第三方架構(比如這裡的Picasso)。
熱身運動——HttpUrlConnection和AsyncTask實現網狀圖片下載
在使用Picasso架構之前,先來一個熱身運動:使用HttpUrlConnection(java.net.HttpUrlConnection)和AsyncTask(android.os.AsyncTask)實現網狀圖片載入。
activity_main.xml檔案方面,LinearLayout的垂直布局中布置兩個控制項:ImageView和Button;Button的點擊事件觸發網狀圖片請求和載入記憶體(即:顯示圖片)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/iv_image" android:layout_width="200dp" android:layout_height="200dp" /> <Button android:onClick="loadImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading..." /></LinearLayout>
AndroidManifest.xml檔案方面,添加網路請求的使用者權限。
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity.java方面:
package com.fenix.picassodemo;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.View;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView mIView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mIView = (ImageView) findViewById(R.id.iv_image); } private class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> { ImageView iv; public ImageDownloadTask(ImageView iv) { this.iv = iv; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Bitmap doInBackground(String... imgAddress) { URL imgUrl = null; HttpURLConnection connection = null; InputStream inputStream = null; Bitmap bitmap = null; try { imgUrl = new URL(imgAddress[0]); // 將網狀圖片路徑轉化為URL執行個體 connection = (HttpURLConnection) imgUrl.openConnection(); connection.setDoInput(true); connection.connect(); inputStream = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); iv.setImageBitmap(result); } } public void loadImage(View v) { new ImageDownloadTask(mIView).execute("http://pic32.nipic.com/20130829/12906030_124355855000_2.png"); }}
點擊布局檔案中的Button,執行loadImage(),即網狀圖片請求;網路請求不應該被寫在主線程中,因此需要藉助AsyncTask工具(封裝Handler和Thread);一旦點擊Button,即執行ImageDownloadTask。
圖片資源來自:www.baidu.com的圖片庫(右鍵-》複製圖片地址)。
AsyncTask結合HttpURLConnection,確實可以實現網狀圖片的請求,但問題是:Android應用程式(從網狀圖片角度),還比如:管理圖片下載請求,合理使用記憶體等方面都需要考慮(Google工程師——極致體驗的代言人)。這就是為什麼出現了這麼多類似於Picasso圖片請求架構的緣故!當然還有很多開源架構需要熟悉(後續再說唄)。
Picasso架構——初體驗
Android開源架構——Picasso