我们先看一个并不是异步线程加载的例子,使用 Handler+Runnable模式。
这里为何不是新开线程的原因请参看这篇文章:Android Runnable 运行在那个线程 这里的代码其实是在UI 主线程中下载图片的,而不是新开线程。
我们运行下面代码时,会发现他其实是阻塞了整个界面的显示,需要所有图片都加载完成后,才能显示界面。
01
02
03
04
05
06
07
08
09
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
|
package
ghj1976.AndroidTest;
import
java.io.IOException;
import
java.net.URL;
import
android.app.Activity;
import
android.graphics.drawable.Drawable;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.SystemClock;
import
android.util.Log;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadImage(
"http://www.baidu.com/img/baidu_logo.gif"
, R.id.imageView1);
loadImage(<img id=
"\"aimg_wiyCZ\""
onclick=
"\"zoom(this,"
this
.src,=
""
0
,=
""
0
)\
"="
" class="
\"zoom\"
" file="
\"http:
//www.chinatelecom.com.cn/images/logo_new.gif\"" onmouseover="\"img_onmouseoverfunc(this)\"" onload="\"thumbImg(this)\"" border="\"0\"" alt="\"\"">",
R.id.imageView2);
loadImage("http:
//cache.soso.com/30d/img/web/logo.gif, R.id.imageView3);
loadImage(
"http://csdnimg.cn/www/images/csdnindex_logo.gif"
,
R.id.imageView4);
loadImage(
"http://images.cnblogs.com/logo_small.gif"
,
R.id.imageView5);
}
private
Handler handler =
new
Handler();
private
void
loadImage(
final
String url,
final
int
id) {
handler.post(
new
Runnable() {
public
void
run() {
Drawable drawable =
null
;
try
{
drawable = Drawable.createFromStream(
new
URL(url).openStream(),
"image.gif"
);
}
catch
(IOException e) {
Log.d(
"test"
, e.getMessage());
}
if
(drawable ==
null
) {
Log.d(
"test"
,
"null drawable"
);
}
else
{
Log.d(
"test"
,
"not null drawable"
);
}
// 为了测试缓存而模拟的网络延时
SystemClock.sleep(
2000
);
((ImageView) MainActivity.
this
.findViewById(id))
.setImageDrawable(drawable);
}
});
}
}
|
这种模式使用了线程,所以可以看到异步加载的效果。
核心代码:
01
02
03
04
05
06
07
08
09
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
|
package
ghj1976.AndroidTest;
import
java.io.IOException;
import
java.net.URL;
import
android.app.Activity;
import
android.graphics.drawable.Drawable;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.os.SystemClock;
import
android.util.Log;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadImage2(
"http://www.baidu.com/img/baidu_logo.gif"
, R.id.imageView1);
loadImage2(
"http://www.chinatelecom.com.cn/images/logo_new.gif"
,
R.id.imageView2);
loadImage2(
"http://cache.soso.com/30d/img/web/logo.gif"
, R.id.imageView3);
loadImage2(
"http://csdnimg.cn/www/images/csdnindex_logo.gif"
,
R.id.imageView4);
loadImage2(
"http://images.cnblogs.com/logo_small.gif"
,
R.id.imageView5);
}
final
Handler handler2 =
new
Handler() {
@Override
public
void
handleMessage(Message msg) {
((ImageView) MainActivity.
this
.findViewById(msg.arg1))
.setImageDrawable((Drawable) msg.obj);
}
};
// 采用handler+Thread模式实现多线程异步加载
private
void
loadImage2(
final
String url,
final
int
id) {
Thread thread =
new
Thread() {
@Override
public
void
run() {
Drawable drawable =
null
;
try
{
drawable = Drawable.createFromStream(
new
URL(url).openStream(),
"image.png"
);
}
catch
(IOException e) {
Log.d(
"test"
, e.getMessage());
}
// 模拟网络延时
SystemClock.sleep(
2000
);
Message message = handler2.obtainMessage();
message.arg1 = id;
message.obj = drawable;
handler2.sendMessage(message);
}
};
thread.start();
thread =
null
;
}
}
|
这时候我们可以看到实现了异步加载, 界面打开时,五个ImageView都是没有图的,然后在各自线程下载完后才把图自动更新上去。
Handler+ExecutorService(线程池)+MessageQueue模式能开线程的个数毕竟是有限的,我们总不能开很多线程,对于手机更是如此。
这个例子是使用线程池。Android拥有与Java相同的ExecutorService实现,我们就来用它。
线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理。当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程对象所带来的性能开销,节省了系统的资源。
线程池的信息可以参看这篇文章:Java&Android的线程池-ExecutorService 下面的演示例子是创建一个可重用固定线程数的线程池。
核心代码
01
02
03
04
05
06
07
08
09
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
|
package
ghj1976.AndroidTest;
import
java.io.IOException;
import
java.net.URL;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
import
android.app.Activity;
import
android.graphics.drawable.Drawable;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.os.SystemClock;
import
android.util.Log;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadImage3(
"http://www.baidu.com/img/baidu_logo.gif"
, R.id.imageView1);
loadImage3(
"http://www.chinatelecom.com.cn/images/logo_new.gif"
,
R.id.imageView2);
loadImage3(
"http://cache.soso.com/30d/img/web/logo.gif"
,
R.id.imageView3);
loadImage3(
"http://csdnimg.cn/www/images/csdnindex_logo.gif"
,
R.id.imageView4);
loadImage3(
"http://images.cnblogs.com/logo_small.gif"
,
R.id.imageView5);
}
private
Handler handler =
new
Handler();
private
ExecutorService executorService = Executors.newFixedThreadPool(
5
);
// 引入线程池来管理多线程
private
void
loadImage3(
final
String url,
final
int
id) {
executorService.submit(
new
Runnable() {
public
void
run() {
try
{
final
Drawable drawable = Drawable.createFromStream(
new
URL(url).openStream(),
"image.png"
);
// 模拟网络延时
SystemClock.sleep(
2000
);
handler.post(
new
Runnable() {
public
void
run() {
((ImageView) MainActivity.
this
.findViewById(id))
.setImageDrawable(drawable);
}
});
}
catch
(Exception e) {
|