Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 562|回复: 0

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

[复制链接]

该用户从未签到

发表于 2011-10-22 12:29:26 | 显示全部楼层 |阅读模式
异步加载远程图片的小DEMO,欢迎拍砖
以下是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: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) {}

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-12-23 09:11 , Processed in 0.934063 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表