以下是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 图片高斯模糊
  2. Android(安卓)获取当前设备的IP地址
  3. android 多媒体之 MediaStore 学习记录
  4. Error:Execution failed for task ':app:transformClassesWithJa
  5. android 4.4 webview加载部分https网页白屏
  6. Android(安卓)Gallery(超豪华胜优酷,自动无限循环播放+3D+阴影,
  7. android图片缩放(指定大小)
  8. android 图片LOMO效果
  9. android 调用相机保存拍照后的图片

随机推荐

  1. 解析Android中的main线程与子线程
  2. ADB 工具
  3. Android的RelativeLayout的layout_height
  4. Android应用程序内存分析-Memory Analysi
  5. -如何将Eclipse中的项目迁移到Android St
  6. Android(安卓)实例教程
  7. 高焕堂
  8. Android 基于google Zxing实现二维码、条
  9. Android Camera
  10. 2011年Android产品趋势