1.新建android工程asyncloadpicture
新建布局文件activity_main.xml主界面为一个gridview,还有其子项布局文件gridview_item.xml
2.功能主界面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
|
package com.example.asyncloadpicture; import java.util.arraylist; import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.asynctask; import android.os.bundle; import android.widget.gridview; /** * @author ymw * @data -- */ public class mainactivity extends activity { private gridview gridviewimg; private arraylist<imageentity> imagelist = new arraylist<imageentity>(); @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridviewimg = (gridview) findviewbyid(r.id.gridviewimg); initdata(); } private void initdata() { for ( int i = ; i < ; i++) { imageentity b = new imageentity(); b.setimage(bitmapfactory.decoderesource(getresources(), r.drawable.nopicture)); imagelist.add(b); } imageadapter adapter = new imageadapter(getapplicationcontext(), imagelist); gridviewimg.setadapter(adapter); string[] params = { "http://images.cnitblog.com/i///.png" , "p" }; new imageloadtask(getapplicationcontext(), adapter).execute(params); } /** 图片异步加载类 */ public class imageloadtask extends asynctask<string, void , void > { private imageadapter adapter; public imageloadtask(context context, imageadapter adapter) { this .adapter = adapter; } @override protected void doinbackground(string... params) { string url = params[]; // 获取传来的参数,图片uri地址 string p = params[]; for ( int i = ; i < adapter.getcount(); i++) { imageentity bean = (imageentity) adapter.getitem(i); bitmap bitmap = bitmapfactory.decodestream(request .handlerdata(url)); bean.setimage(bitmap); publishprogress(); // 通知去更新ui } return null ; } public void onprogressupdate( void ... voids) { if (iscancelled()) return ; // 更新ui adapter.notifydatasetchanged(); } } /** 图片实体 */ public class imageentity { private bitmap image; public bitmap getimage() { return image; } public void setimage(bitmap image) { this .image = image; } } } |
3.gridview列表图片适配器代码imageadapter.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
|
package com.example.asyncloadpicture; import java.util.arraylist; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import com.example.asyncloadpicture.mainactivity.imageentity; /** * * @author ymw * @data -- 图片适配器 */ public class imageadapter extends baseadapter { private context _context; private arraylist<imageentity> _list; public imageadapter(context context, arraylist<imageentity> imagelist) { this ._context = context; this ._list = imagelist; } @override public int getcount() { return _list.size(); } @override public object getitem( int position) { return _list.get(position); } @override public long getitemid( int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { layoutinflater inflater = layoutinflater.from(_context); convertview = inflater.inflate(r.layout.gridview_item, null ); imageview imgview = (imageview) convertview.findviewbyid(r.id.imgview); imageentity b = _list.get(position); // 如果有图片则读取,没有则跳过 if (b.getimage() != null ) { imgview.setimagebitmap(b.getimage()); } return convertview; } } |
4.请求网络读取图片的类request.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
|
package com.example.asyncloadpicture; import java.io.ioexception; import java.io.inputstream; import java.net.url; import java.net.urlconnection; import android.graphics.drawable.drawable; import android.util.log; /** * 工具类,主要根据url读取图片返回流的方法 * * @author ymw * */ public class request { public static inputstream handlerdata(string url) { inputstream instream = null ; try { url feedurl = new url(url); urlconnection conn = feedurl.openconnection(); conn.setconnecttimeout( * ); instream = conn.getinputstream(); } catch (exception e) { e.printstacktrace(); } return instream; } /** 直接返回drawable对象设置图片 */ public static drawable loadimagefromnetwork(string imageurl) { drawable drawable = null ; try { // 可以在这里通过文件名来判断,是否本地有此图片 drawable = drawable.createfromstream( new url(imageurl).openstream(), "image.jpg" ); } catch (ioexception e) { log.d( "test" , e.getmessage()); } if (drawable == null ) { log.d( "test" , "null drawable" ); } else { log.d( "test" , "not null drawable" ); } return drawable; } } |
以上内容给大家介绍了android 中使用 asynctask 异步读取网络图片的相关知识,希望对大家有所帮助!