日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android异步下载网络图片(其一:Handler)

發(fā)布時間:2025/6/15 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android异步下载网络图片(其一:Handler) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

項目中有時候需要獲取網(wǎng)絡(luò)上的圖片,并下載下來到手機客戶端顯示。怎么做呢?

實現(xiàn)思路是:

?1:在UI線程中啟動一個線程,讓這個線程去下載圖片。

?2:圖片完成下載后發(fā)送一個消息去通知UI線程

?2:UI線程獲取到消息后,更新UI。

?這里的UI線程就是主線程。

?這兩個步驟涉及到一些知識點,即是:ProgressDialog,Handler,Thread/Runnable,URL,HttpURLConnection等等一系列東東的使用。

?現(xiàn)在讓我們開始來實現(xiàn)這個功能吧!

?第一步:新建項目。

?第二步:設(shè)計好UI,如下所示

View Code <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <Buttonandroid:id="@+id/btnFirst"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="異步下載方式一"></Button><Buttonandroid:id="@+id/btnSecond"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="異步下載方式二"></Button><FrameLayoutandroid:layout_width="fill_parent"android:layout_height="match_parent"android:id="@+id/frameLayout"><ImageViewandroid: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> 

第三步:獲取UI相應(yīng)View組件,并添加事件監(jiān)聽。

View Code public class DownLoaderActivity extends Activity implements OnClickListener{ private static final String params="http://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;@Overridepublic 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); }

第四步:在監(jiān)聽事件中處理我們的邏輯,即是下載服務(wù)器端圖片數(shù)據(jù)。

這里我們需要講解一下了。

通常的我們把一些耗時的工作用另外一個線程來操作,比如,下載上傳圖片,讀取大批量XML數(shù)據(jù),讀取大批量sqlite數(shù)據(jù)信息。為什么呢?答案大家都明白,用戶體驗問題。

在這里,首先我構(gòu)造一個進度條對話框,用來顯示下載進度,然后開辟一個線程去下載圖片數(shù)據(jù),下載數(shù)據(jù)完畢后,通知主UI線程去更新顯示我們的圖片。

Handler是溝通Activity 與Thread/runnable的橋梁。而Handler是運行在主UI線程中的,它與子線程可以通過Message對象來傳遞數(shù)據(jù)。具體代碼如下:

View Code /**這里重寫handleMessage方法,接受到子線程數(shù)據(jù)后更新UI**/private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg){switch(msg.what){case 1://關(guān)閉 ImageView view=(ImageView)frameLayout.findViewById(R.id.image);view.setImageBitmap(bitmap);dialog.dismiss();break;}}};



我們在這里彈出進度對話框,使用HTTP協(xié)議來獲取數(shù)據(jù)。

View Code //前臺ui線程在顯示ProgressDialog,//后臺線程在下載數(shù)據(jù),數(shù)據(jù)下載完畢,關(guān)閉進度框 @Overridepublic void onClick(View view) {switch(view.getId()){case R.id.btnFirst: dialog = ProgressDialog.show(this, "", "下載數(shù)據(jù),請稍等 …", true, true); //啟動一個后臺線程 handler.post(new Runnable(){@Overridepublic void run() { //這里下載數(shù)據(jù) 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) {// TODO Auto-generated catch block e.printStackTrace();} }}); break;

如此以來,你會發(fā)現(xiàn)很好的完成了我們的下載目標(biāo)了,你可以把它應(yīng)用到其他方面去,舉一反三。

運行截圖如下:

?

總結(jié)

以上是生活随笔為你收集整理的Android异步下载网络图片(其一:Handler)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。