欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 异步下载图片

程序员文章站 2022-06-30 11:32:54
...

SyncImageLoader 图片异步下载工具类 

package mars.async.down.utils;

import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

public class SyncImageLoader {
	private HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();

	// 根据URL从网络上下载图片,生成Drawable对象
	public Drawable loadImageFormUrl(String imageUrl) {
		try {
			return Drawable.createFromStream(new URL(imageUrl).openStream(),
					"src");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public Drawable loadDrawable(final String imageUrl,
			final ImageCallback imageCallback) {
		// 判断缓存中是否有imageUrl这个缓存存在
		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			if (softReference.get() != null) {
				return softReference.get();
			}
		}

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				imageCallback.imageLoaded((Drawable) msg.obj);
			}

		};

		// 缓存中不存在的话,启动异步线程下载图片
		new Thread() {

			@Override
			public void run() {
				// 将图片放入缓存
				Drawable drawable = loadImageFormUrl(imageUrl);
				imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
				Message msg = handler.obtainMessage(0, drawable);
				handler.sendMessage(msg);
			}

		}.start();

		return null;
	}

	// 回调函数,图片加载完毕后调用该函数
	public interface ImageCallback {
		public void imageLoaded(Drawable imageDrawable);
	}

}

 
CallbackImplements 回调接口实现类。

package mars.async.down.utils;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public class CallbackImplements implements SyncImageLoader.ImageCallback {

	private ImageView imageView;

	public CallbackImplements(ImageView imageView) {
		super();
		this.imageView = imageView;
	}

	@Override
	public void imageLoaded(Drawable imageDrawable) {
		imageView.setImageDrawable(imageDrawable);
	}

}

 
在Activity中调用

package mars.async.down;

import mars.async.down.utils.CallbackImplements;
import mars.async.down.utils.SyncImageLoader;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private SyncImageLoader syncImageLoader = new SyncImageLoader();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		loadImage("http://www.android.com/images/icon-partners.png",
				R.id.imageView1);
		loadImage("http://www.android.com/images/icon-dev.png", R.id.imageView2);
		loadImage("http://www.android.com/images/icon-market.png",
				R.id.imageView3);
	}

	// url:图片的url地址
	// id:ImageView控件的ID
	private void loadImage(String url, int id) {
		ImageView imageView = (ImageView) findViewById(id);
		CallbackImplements callbackImplements = new CallbackImplements(
				imageView);
		Drawable cacheImage = syncImageLoader.loadDrawable(url,
				callbackImplements);
		// 如果图片缓存存在,则在外部设置image.否则是调用的callback函数设置
		if (cacheImage != null) {
			imageView.setImageDrawable(cacheImage);
		}
	}

}