android项目源码异步加载远程图片的小例子

package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 异步加载远程图片VIEW
 * 
 * @author <a href="mailto:admin@mrjeye.org">Mr.J</a> 2011-3-30
 * 
 * @version 1.0
 */
public class RemoteImageView extends SurfaceView implements
		SurfaceHolder.Callback {
	private static final String TAG = RemoteImageView.class.getName();
	private SurfaceHolder holder = null;
	// 图片数据(通过加载而得)
	private byte[] data;
	// 要加载的图片URL
	private String url;
	// 标识当前图片是否正在加载过程中
	private boolean loading;

	public RemoteImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		holder = getHolder();
		holder.addCallback(this);
		if (attrs != null) {
			url = attrs.getAttributeValue(null, "url");
		}
		if (url == null || url.equals("")) {
			return;
		}
		loadImageData(url);
	}

	public RemoteImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RemoteImageView(Context context) {
		this(context, null);
	}

	/**
	 * 异步加载图片
	 * 
	 * @param url
	 *            加载的图片URL
	 */
	private void loadImageData(final String url) {
		loading = true;
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
				}
				_draw(); // 打开欢迎信息
				data = getByteByUrl(url);
				loading = false;
				_draw();
			}
		}.start();
	}

	/**
	 * 设置新的图片
	 * 
	 * @param url
	 */
	public void setUrl(String url) {
		this.url = url;
		if (url == null || url.equals("")) {
			Log.d(TAG, "the remote url can not be null");
			return;
		}
		Log.d(TAG, "set remote image url : " + url);
		loadImageData(url);
	}

	/**
	 * 画当前状态或已加载成功的图片
	 * 
	 */
	private void _draw() {
		Canvas canvas = holder.lockCanvas(null);
		if (canvas == null) {
			Log.d(TAG, "can not lock canvas from holder");
			return;
		}
		__draw(canvas);
		holder.unlockCanvasAndPost(canvas);
	}

	private void __draw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		if (loading) {
			canvas.drawText("Loading …", 0, 20, paint);
			return;
		}
		if (data == null || data.length == 0) {
			Log.d(TAG, "can not drawing the remote image : data is null");
			canvas.drawText("the url (" + url + ") is not a image resource", 0,
					20, paint);
			return;
		}
		try {
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			canvas.drawColor(Color.BLACK);
			canvas.drawBitmap(bitmap, null, new Rect(0, 0, getWidth(),
					getHeight()), new Paint());
		} catch (Exception e) {
			canvas.drawText(e.getMessage(), 0, 20, paint);
		}
		Log.d(TAG, "draw completed, data len : " + data.length);
	}

	/**
	 * 加载图片流
	 * 
	 * @param url
	 *            图片地址
	 * 
	 * @return
	 */
	private byte[] getByteByUrl(String url) {
		URL _url = null;
		HttpURLConnection connection = null;
		InputStream is = null;
		ByteArrayOutputStream bos = null;
		byte[] ret = null;
		try {
			_url = new URL(url);
			connection = (HttpURLConnection) _url.openConnection();
			connection.setDoInput(true);
			is = connection.getInputStream();
			bos = new ByteArrayOutputStream();
			byte[] buff = new byte[512];
			int len = -1;
			while ((len = is.read(buff)) != -1) {
				bos.write(buff, 0, len);
			}
			ret = bos.toByteArray();
		} catch (Exception e0) {
			Log.e(TAG, e0.getMessage());
			return null;
		} finally {
			try {
				bos.close();
			} catch (Exception e) {
			}
			;
			try {
				is.close();
			} catch (Exception e) {
			}
			;
			try {
				connection.disconnect();
			} catch (Exception e) {
			}
			;
		}
		return ret;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值