編輯:Android 開發實例
專案中時常需要從網路取得圖片並下載到手機客戶端顯示。該如何實作呢?
整體思路如下:
1:在 UI 執行緒中啟動一個執行緒,讓它負責下載圖片。
2:圖片下載完成後,發送一個訊息通知 UI 執行緒。
3:UI 執行緒接收到訊息後,更新介面。
這裡的 UI 執行緒就是主執行緒。整個過程涉及 ProgressDialog、Handler、Thread/Runnable、URL、HttpURLConnection 等一系列知識點。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnFirst" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="非同步下載方式一" > </Button> <Button android:id="@+id/btnSecond" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="非同步下載方式二" > </Button> <FrameLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:id="@+id/frameLayout" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:padding="2dp" > </ImageView> <ProgressBar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </ProgressBar> </FrameLayout> </LinearLayout>
public class DownLoaderActivity extends Activity implements OnClickListener{
private static final String params="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";
private Button btnFirst,btnSecond;
private ProgressBar progress;
private FrameLayout frameLayout;
private Bitmap bitmap=null;
ProgressDialog dialog=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFirst=(Button)this.findViewById(R.id.btnFirst);
btnSecond=(Button)this.findViewById(R.id.btnSecond);
progress=(ProgressBar)this.findViewById(R.id.progress);
progress.setVisibility(View.GONE);
frameLayout=(FrameLayout)this.findViewById(R.id.frameLayout);
btnFirst.setOnClickListener(this);
btnSecond.setOnClickListener(this);
}
我們通常會把耗時的工作放到另一個執行緒,例如上傳下載圖片、讀取大量 XML 或 SQLite 資料,主要目的是為了良好的使用者體驗。
在這裡,我們先建立一個進度條對話框顯示下載進度,再開闢一個執行緒下載圖片,完成後通知主 UI 執行緒更新顯示。
Handler 是溝通 Activity 與 Thread/Runnable 的橋樑,它運行於主 UI 執行緒,能與子執行緒透過 Message 物件傳遞資料:
/**重寫 handleMessage 方法,接收子執行緒資料後更新 UI**/
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg){
switch(msg.what){
case 1:
ImageView view=(ImageView)frameLayout.findViewById(R.id.image);
view.setImageBitmap(bitmap);
dialog.dismiss();
break;
}
}
};
接著在點擊事件中彈出進度對話框,並以 HTTP 協議取得資料:
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnFirst:
dialog = ProgressDialog.show(this, "",
"下載資料,請稍候 …", true, true);
//啟動一個背景執行緒
handler.post(new Runnable(){
@Override
public void run() {
try{
URL url = new URL(params);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream inputStream=conn.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
break;
如此一來,我們便能順利完成圖片下載的目標。讀者可將這套思路舉一反三,套用到其他非同步處理的情境中。
Android 多語言支援入門:values 資料夾與 strings.xml 的組織方式
一個上架到 Google Play 的 App,可能同時被德國、日本、巴西的使用者安裝。如果畫面上的字永遠只有英文,對不懂英文的使用者來說,這個 App 等於不存在。本地
Android MediaPlayer 教學:打造可播放、暫停、快轉的音樂播放器
Android 提供了多種播放音訊與影片的方式,其中最直接、也最常被拿來播放應用程式內建媒體檔案的,就是 MediaPlayer 這個類別。它其實是內建媒體播放服務的一層
ListView 分組實作:像通訊錄一樣按首字母分類
好的資訊呈現,講究的是「讓使用者一眼看到最有價值的部分」。而當資料量一大,分組就從加分項變成必需品。 分組的應用場合非常廣——只要是集合型資料,幾乎都適合分組。實作形式
現代 Android 架構實戰:從響應式資料流到建置系統的整理術
沉寂了一段時間,收集到不少讀者回饋之後,我覺得這個話題該好好談一談了。接下來我想分享的是:在我眼中,打造一個現代化 Android 應用該有的樣子,以及支撐它背後的架構思