日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 嵌入式 > 嵌入式軟件
[導(dǎo)讀] 實(shí)現(xiàn)思路是:1:在UI線程中啟動(dòng)一個(gè)線程,讓這個(gè)線程去下載圖片。2:圖片完成下載后發(fā)送一個(gè)消息去通知UI線程2:UI線程獲取到消息后,更新UI。這里的UI線程就是主線程。這兩

 實(shí)現(xiàn)思路是:

1:在UI線程中啟動(dòng)一個(gè)線程,讓這個(gè)線程去下載圖片。

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

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

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

這兩個(gè)步驟涉及到一些知識(shí)點(diǎn),即是:ProgressDialog,Handler,Thread/Runnable,URL,HttpURLConnection等等一系列東東的使用。

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

第一步:新建項(xiàng)目。

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

View Code

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

 

android:id="@+id/btnFirst"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="異步下載方式一"

>

 

 

android:id="@+id/btnSecond"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="異步下載方式二"

>

 

 

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:id="@+id/frameLayout"

>

 

android:id="@+id/image"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerInside"

android:padding="2dp"

>

 

 

android:id="@+id/progress"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center">

 

 

 



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

View Codepublic class DownLoaderActivityextends Activityimplements 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;

@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);

}

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

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

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

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

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

View Code

private Handler handler=new Handler(){

@Override

public 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;

}

}

};



我們?cè)谶@里彈出進(jìn)度對(duì)話框,使用HTTP協(xié)議來獲取數(shù)據(jù)。

View Code//前臺(tái)ui線程在顯示ProgressDialog,

//后臺(tái)線程在下載數(shù)據(jù),數(shù)據(jù)下載完畢,關(guān)閉進(jìn)度框

@Override

public void onClick(View view) {

switch(view.getId()){

case R.id.btnFirst:

dialog= ProgressDialog.show(this,"",

"下載數(shù)據(jù),請(qǐng)稍等 …",true,true);

//啟動(dòng)一個(gè)后臺(tái)線程

handler.post(new Runnable(){

@Override

public 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();[!--empirenews.page--]

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

break;

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

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉