<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Android異步加載網(wǎng)絡(luò )圖片

Android異步加載網(wǎng)絡(luò )圖片

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò ) 收藏

實(shí)現思路是:

本文引用地址:http://dyxdggzs.com/article/201609/305034.htm

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

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

2:UI線(xiàn)程獲取到消息后,更新UI。

這里的UI線(xiàn)程就是主線(xiàn)程。

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

現在讓我們開(kāi)始來(lái)實(shí)現這個(gè)功能吧!

第一步:新建項目。

第二步:設計好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相應View組件,并添加事件監聽(tīng)。

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

}

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

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

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

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

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

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;

}

}

};

我們在這里彈出進(jìn)度對話(huà)框,使用HTTP協(xié)議來(lái)獲取數據。

View Code//前臺ui線(xiàn)程在顯示ProgressDialog,

//后臺線(xiàn)程在下載數據,數據下載完畢,關(guān)閉進(jìn)度框

@Override

public void onClick(View view) {

switch(view.getId()){

case R.id.btnFirst:

dialog= ProgressDialog.show(this,,

下載數據,請稍等 …,true,true);

//啟動(dòng)一個(gè)后臺線(xiàn)程

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


上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>