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) {
throw new RuntimeException(e);
}
}
});
}
}
加上緩存
package ghj1976.AndroidTest;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.SystemClock;
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();
/**
*
* @param imageUrl
* 圖像url地址
* @param callback
* 回調接口
* <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 返回內存中緩存的圖像,第一次加載返回null
*/
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() {
public void run() {
try {
final Drawable drawable = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(
drawable));
handler.post(new Runnable() {
public void run() {
callback.imageLoaded(drawable);
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
return null;
}
// 從網絡上取數據方法
protected 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);
}
}