一、简介
二、代码流程
1.private Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();来起到缓存图片的作用
2.每当要访问图片,先在map找,找不到则new Thread().start(),访问网络得到图片,再传给handler,
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
3.handler若收到message 就产生回调,没轩imageView的图片信息callback.imageLoaded((Drawable) msg.obj);
三、代码
1.xml
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/imageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:id="@+id/imageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:id="@+id/imageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>
(2)AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
2.java
(1)MainActivity.java
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity {
/** Called when the activity is first created. */
private AsyncImageLoader loader = new AsyncImageLoader();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadImage("http://www.android.com/images/icon-partners.png", R.id.imageView01);
loadImage("http://www.android.com/images/icon-dev.png", R.id.imageView02);
loadImage("http://www.android.com/images/icon-market.png", R.id.imageView03);
}
//url:下载图片的URL
//id:ImageView控件的ID
private void loadImage(final String url, final int id) {
// 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
ImageView imageView = (ImageView)findViewById(id);
//把imageVieww传给CallbackImpl,回调时就可以设置图片
CallbackImpl callbackImpl = new CallbackImpl(imageView);
Drawable cacheImage =
loader.loadDrawable(url, callbackImpl);
if (cacheImage != null) {
imageView.setImageDrawable(cacheImage);
}
}
}
(2)AsyncImageLoader.java
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map; import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message; //该类的主要作用是实现图片的异步加载
public class AsyncImageLoader {
//图片缓存对象
//键是图片的URL,值是一个SoftReference对象,该对象指向一个Drawable对象
//这样并不影响image被garbage collecter回收
private Map<String, SoftReference<Drawable>> imageCache =
new HashMap<String, SoftReference<Drawable>>(); //实现图片的异步加载
public Drawable loadDrawable(final String imageUrl,final ImageCallback callback){
//查询缓存,查看当前需要下载的图片是否已经存在于缓存当中
if(imageCache.containsKey(imageUrl)){
SoftReference<Drawable> softReference=imageCache.get(imageUrl);
if(softReference.get() != null){//如果为null,则说明被垃圾回收器回收了
return softReference.get();
}
} //Handler和UI是同一线程的
final Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
//msg.obj是handler.obtainMessage(0, drawable);传过来的
callback.imageLoaded((Drawable) msg.obj);
}
}; //如果缓存没有,则新开辟一个线程,该线程用于进行图片的下载
//每下载一个图片,都 会开辟一个新线程,和UI不是同一线程的
new Thread(){
public void run() {
Drawable drawable=loadImageFromUrl(imageUrl);
//把图片加到map中,以缓存图片,则下次访问不用访问网络
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
};
}.start();
return null;//这里不能return drawable,因为它是在另一个线程,此时drawable可能还没值
}
//该方法用于根据图片的URL,从网络上下载图片
protected Drawable loadImageFromUrl(String imageUrl) {
try {
//根据图片的URL,下载图片,并生成一个Drawable对象
return Drawable.createFromStream(new URL(imageUrl).openStream(), "src");
} catch (Exception e) {
throw new RuntimeException(e);
}
} //回调接口
public interface ImageCallback{
public void imageLoaded(Drawable imageDrawable);
}
}
(3)CallbackImpl.java
import android.graphics.drawable.Drawable;
import android.widget.ImageView; public class CallbackImpl implements AsyncImageLoader.ImageCallback{
private ImageView imageView ; public CallbackImpl(ImageView imageView) {
super();
this.imageView = imageView;
} @Override
public void imageLoaded(Drawable imageDrawable) {
imageView.setImageDrawable(imageDrawable);
} }