android项目源码异步加载远程图片的小例子
以下是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; // 要加载的图片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) {} } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架