由于原文的结构有些不是很容易看清,先整理如下:
原文:http://www.eoeandroid.com/thread-210082-1-1.html
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.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>
<activity
android:name="com.example.test.MainActivity0"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.test.MainActivity1"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.test.MainActivity2"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.test.MainActivity3"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
菜单Activity(XML+Java文件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="zero"
android:text="0" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="one"
android:text="1" />
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="two"
android:text="2" />
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="three"
android:text="3" />
</LinearLayout>
Java文件
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void zero(View v){
Intent intent = new Intent(MainActivity.this, MainActivity0.class);
startActivity(intent);
}
public void one(View v){
Intent intent = new Intent(MainActivity.this, MainActivity1.class);
startActivity(intent);
}
public void two(View v){
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
public void three(View v){
Intent intent = new Intent(MainActivity.this, MainActivity3.class);
startActivity(intent);
}
}
1Handler+Runnable模式(XML+Java文件,加载图片共用的XML)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/i1"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageView
android:id="@+id/i2"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageView
android:id="@+id/i3"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageView
android:id="@+id/i4"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
public class MainActivity0 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0);
loadImage0("http://www.baidu.com/img/baidu_logo.gif", R.id.i1);
loadImage0("http://www.chinatelecom.com.cn/images/logo_new.gif",
R.id.i2);
loadImage0("http://cache.soso.com/30d/img/web/logo.gif", R.id.i3);
loadImage0("http://csdnimg.cn/www/images/csdnindex_logo.gif", R.id.i4);
}
private Handler handler = new Handler();
private void loadImage0(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) findViewById(id)).setImageDrawable(drawable);
}
});
}
}
分析:此种加载方式会阻塞UI线程,在较新的Android版本,是不允许这样做的,如果UI线程访问网络,会抛出异常:NetworkOnMainThreadException,因此大家看到我的清单文件中的版本是很低的,为了能够让程序运行起来。另外,这种方式需要等4张图片一张张加载完才会显示UI,所以用户体验是相当差的。
2Handler+Thread+Message模式(Java文件)
public class MainActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0);
loadImage2("http://www.baidu.com/img/baidu_logo.gif", R.id.i1);
loadImage2("http://www.chinatelecom.com.cn/images/logo_new.gif",
R.id.i2);
loadImage2("http://cache.soso.com/30d/img/web/logo.gif", R.id.i3);
loadImage2("http://csdnimg.cn/www/images/csdnindex_logo.gif", R.id.i4);
}
final Handler handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
((ImageView) findViewById(msg.arg1))
.setImageDrawable((Drawable) msg.obj);
}
};
public void loadImage2(final String url, final int id) {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(
new URL(url).openStream(), "image.png");
// 模拟网络延时
SystemClock.sleep(2000);
// 更新UI需要放到主线程
Message message = handler2.obtainMessage();
message.arg1 = id;
message.obj = drawable;
handler2.sendMessage(message);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
thread = null;
}
}
* 分析 * :同时开启4个thread访问图片
3Handler+ExecutorService(线程池)+MessageQueue模式(Java文件)
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0);
loadImage3("http://www.baidu.com/img/baidu_logo.gif", R.id.i1);
loadImage3("http://www.chinatelecom.com.cn/images/logo_new.gif",
R.id.i2);
loadImage3("http://cache.soso.com/30d/img/web/logo.gif", R.id.i3);
loadImage3("http://csdnimg.cn/www/images/csdnindex_logo.gif", R.id.i4);
}
private ExecutorService executorService = Executors.newFixedThreadPool(5);
private Handler handler = new Handler();
// 引入线程池来管理多线程
public void loadImage3(final String url, final int id) {
executorService.submit(new Runnable() {
@Override
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() {
if (null != drawable) {
((ImageView) findViewById(id))
.setImageDrawable(drawable);
}
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
分析:
能开线程的个数毕竟是有限的,我们总不能开很多线程,对于手机更是如此。
这个例子是使用线程池。Android拥有与Java相同的ExecutorService实现,我们就来用它。
线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理。当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程对象所带来的性能开销,节省了系统的资源。
线程池的信息可以参看这篇文章:Java&Android的线程池-ExecutorService(应该是这篇)演示例子是创建一个可重用固定线程数的线程池。
4Handler+ExecutorService(线程池)+MessageQueue+缓存模式(Java文件)
public class AsyncImageLoader3 {
// 为了加快速度,在内存中开启缓存(主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动)
public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
private ExecutorService executorService = Executors.newFixedThreadPool(5); // 固定五个线程来执行任务
private final Handler handler = new Handler();
public Drawable loadDrawable(final String imageUrl,
final ImageCallback callback) {
// 如果缓存过就从缓存中取出数据
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
if (softReference.get() != null) {
return softReference.get();
}
}
// 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
executorService.submit(new Runnable() {
@Override
public void run() {
final Drawable drawable = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
handler.post(new Runnable() {
public void run() {
callback.imageLoaded(drawable);
}
});
}
});
return null;
}
// 从网络上取数据方法
public Drawable loadImageFromUrl(String imageUrl) {
try {
// 测试时,模拟网络延时,实际时这行代码不能有
SystemClock.sleep(2000);
return Drawable.createFromStream(new URL(imageUrl).openStream(),
"image.png");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 对外界开放的回调接口
public interface ImageCallback {
// 注意 此方法是用来设置目标对象的图像资源
public void imageLoaded(Drawable imageDrawable);
}
}
public class MainActivity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main0);
loadImage4("http://www.baidu.com/img/baidu_logo.gif", R.id.i1);
loadImage4("http://www.chinatelecom.com.cn/images/logo_new.gif",
R.id.i2);
loadImage4("http://cache.soso.com/30d/img/web/logo.gif", R.id.i3);
loadImage4("http://csdnimg.cn/www/images/csdnindex_logo.gif", R.id.i4);
}
private AsyncImageLoader3 asyncImageLoader3 = new AsyncImageLoader3();
// 引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程
private void loadImage4(final String url, final int id) {
// 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
Drawable cacheImage = asyncImageLoader3.loadDrawable(url,
new AsyncImageLoader3.ImageCallback() {
// 请参见实现:如果第一次加载url时下面方法会执行
public void imageLoaded(Drawable imageDrawable) {
((ImageView) findViewById(id))
.setImageDrawable(imageDrawable);
}
});
if (cacheImage != null) {
((ImageView) findViewById(id)).setImageDrawable(cacheImage);
}
}
}
比起前一个做了几个改造:
- 把整个代码封装在一个类中
- 为了避免出现同时多次下载同一幅图的问题,使用了本地缓存
另外还记得享元模式么,这边第四种加载图片方式其实就是享元模式的运用,记不得请看这里。