Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 非同步下載網路圖片(其一):Handler 與執行緒的基礎實作

編輯:Android 開發實例

專案中時常需要從網路取得圖片並下載到手機客戶端顯示。該如何實作呢?

整體思路如下:

1:在 UI 執行緒中啟動一個執行緒,讓它負責下載圖片。

2:圖片下載完成後,發送一個訊息通知 UI 執行緒。

3:UI 執行緒接收到訊息後,更新介面。

這裡的 UI 執行緒就是主執行緒。整個過程涉及 ProgressDialog、Handler、Thread/Runnable、URL、HttpURLConnection 等一系列知識點。

第一步:建立專案並設計 UI

<?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>

第二步:取得 View 元件並掛上事件監聽

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;

如此一來,我們便能順利完成圖片下載的目標。讀者可將這套思路舉一反三,套用到其他非同步處理的情境中。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved