android手機上,我們常用imageview顯示圖片,我們本章獲取網絡圖片并顯示在imageview中。
一、設計界面
1、布局文件
打開res/layout/activity_main.xml文件。
輸入以下代碼:
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "." encoding= "utf-" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" > <imageview android:id= "@+id/imagephoto" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </linearlayout> |
二、程序文件
打開“src/com.genwoxue.networkphoto/mainactivity.java”文件。
然后輸入以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package com.genwoxue.networkphoto; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.asynctask; import android.os.bundle; import android.widget.imageview; public class mainactivity extends activity { private imageview imview; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imview = (imageview) findviewbyid(r.id.imagephoto); string imageurl = "http://img.baidu.com/img/image/ilogob.gif" ; new networkphoto().execute(imageurl); } /* 四個步驟: * ()onpreexecute(),執行預處理,它運行于ui線程, * 可以為后臺任務做一些準備工作,比如繪制一個進度條控件。 * ()doinbackground(params...),后臺進程執行的具體計算在這里實現, * doinbackground(params...)是asynctask的關鍵,此方法必須重載。 * 在這個方法內可以使用 publishprogress(progress...)改變當前的進度值。 * ()onprogressupdate(progress...),運行于ui線程。如果 * 在doinbackground(params...) 中使用了publishprogress(progress...),就會 * 觸發這個方法。在這里可以對進度條控件根據進度值做出具體的響應。 * ()onpostexecute(result),運行于ui線程,可以對后臺任務的結果做出處理,結果 * 就是doinbackground(params...)的返回值。此方法也要經常重載,如果result為 * null表明后臺任務沒有完成(被取消或者出現異常)。 * */ //本案例我們僅使用了()和() class networkphoto extends asynctask<string, integer, bitmap> { public networkphoto() { } //doinbackground(params...),后臺進程執行的具體計算在這里實現,是asynctask的關鍵,此方法必須重載。 @override protected bitmap doinbackground(string... urls) { url url = null ; bitmap bitmap = null ; httpurlconnection conn= null ; inputstream is= null ; try { url = new url(urls[]); } catch (malformedurlexception e) { e.printstacktrace(); } try { conn = (httpurlconnection) url.openconnection(); conn.setdoinput( true ); conn.connect(); is = conn.getinputstream(); bitmap = bitmapfactory.decodestream(is); is.close(); } catch (ioexception e) { e.printstacktrace(); } finally { if (conn!= null ){ conn.disconnect(); conn= null ; } if (is!= null ) { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } is= null ; } } return bitmap; } //onpostexecute(result),運行于ui線程,可以對后臺任務的結果做出處理,結果 //就是doinbackground(params...)的返回值。 @override protected void onpostexecute(bitmap bitmap) { // 返回結果bitmap顯示在imageview控件 imview.setimagebitmap(bitmap); } } } |
三、配置文件
打開“androidmanifest.xml”文件。
然后輸入以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "." encoding= "utf-" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.genwoxue.networkphoto" android:versioncode= "" android:versionname= "." > <uses-sdk android:minsdkversion= "" android:targetsdkversion= "" /> <uses-permission android:name= "android.permission.internet" /> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "com.genwoxue.networkphoto.mainactivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> |
注意:需要在androidmanifest.xml文件中添加權限:
1
|
<uses-permission android:name= "android.permission.internet" /> |