以下是VIEW代码

package org.mrjeye.android.image;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:<a href="%5C%22mailto:admin@mrjeye.org%5C%22">admin@mrjeye.org</a>”>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;// 要加载的图片URLprivate 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(){@Overridepublic 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;}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}}

更多相关文章

  1. android 图片LOMO效果
  2. android 4.4 webview加载部分https网页白屏
  3. android 图片高斯模糊
  4. android 调用相机保存拍照后的图片
  5. android图片缩放(指定大小)
  6. Android遍历某个文件夹的图片并实现滑动查看的的Gallery
  7. Android 显示大图片
  8. android设置一个图片为全屏大小

随机推荐

  1. Facebook Buck和xctool:针对Android和iOS
  2. html5 canvas android bug
  3. Android入门教程(五)之------第一个Andro
  4. Android(安卓)简单快捷高效的屏幕适配
  5. 一、android集成百度地图(android百度地
  6. 第七章 ARM 反汇编基础(一)(Android(安卓)与
  7. Android导入项目时Android(安卓)jar包丢
  8. Android(安卓)studio安装配置常见问题及
  9. 一种基于Android智能手机的远程视频监控
  10. 浅析Android位置权限以及数组寻找索引的