class imageViewTask{
String path;
Bitmap bitmap;
int position;
}
// 任务集合 private List<imageViewTask> imageviews = new ArrayList<MusicAdapter.imageViewTask>();
// 获得图片bitmap
workThread = new Thread(){
public void run() {
while(isLoop){
if(!imageviews.isEmpty()){
try {
Message msg = new Message();
// 获得图片的bitmap
msg.obj = GetImageviewBitmap();
msg.what = HANDLER_LOAD_IMAGEVIEW_SUCCESS;
// 发消息给主线程
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}else{
synchronized (workThread) {
try {
//任务队列为空则等待
workThread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//GetImageviewBitmap()方法
// 获得图片的bitmap
private imageViewTask GetImageviewBitmap() throws Exception, IOException {
imageViewTask ivt = imageviews.remove(0);
String uri = BasicUri.BasicHttpUri+ivt.path;
HttpEntity entity = new HttpUtils().SetHttp(uri, HttpUtils.GET_METHOD, null);
byte[] bytes = EntityUtils.toByteArray(entity);
Bitmap bitmap = BitmapUtils.loadBitmap(bytes, 50, 50);
// 将网上下载的图片存入缓存集合中
map.put(ivt.path, new SoftReference<Bitmap>(bitmap));
ivt.bitmap = bitmap;
return ivt;
}
};
workThread.start();
}
public class HttpUtils {
public final static int GET_METHOD = 1;
public final static int POST_MEHTOD = 2;
/**
* 构造方法
* @param uri 路径
* @param method 发送消息模式 GET_METHOD用get方式传送消息 POST_MEHTOD用post方式传送消息
*/
public static HttpEntity SetHttp(String uri, int method, List<NameValuePair> pairs) throws Exception{
HttpClient client=new DefaultHttpClient();
HttpResponse resp = null;
switch (GET_METHOD) {
// 用get方式发送消息
case GET_METHOD:
HttpGet get=new HttpGet(uri);
resp=client.execute(get);
break;
// 用post方式发送消息
case POST_MEHTOD:
HttpPost post=new HttpPost(uri);
HttpEntity entity=new UrlEncodedFormEntity( pairs, "utf-8");
post.setEntity(entity);
post.setHeader("Content-Type",
"application/x-www-form-urlencoded");
resp=client.execute(post);
break;
}
return resp.getEntity();
}
}
// 给imageview设置标记
holder.iv.setTag(position);
// 增加任务队列
imageViewTask task = new imageViewTask();
task.path = musics.get(position).getAlbumpic();
task.position = position;
imageviews.add(task);
// 通知工作线程可以下载图片了
synchronized (workThread) {
workThread.notify();
}
// handler
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case HANDLER_LOAD_IMAGEVIEW_SUCCESS:
// 更新UI
imageViewTask ivt = (imageViewTask) msg.obj;
ImageView iv = (ImageView) listview.findViewWithTag(ivt.position);
if(iv != null){
if(ivt.bitmap != null)
iv.setImageBitmap(ivt.bitmap);
}else{
iv.setImageResource(R.drawable.ic_launcher);
}
break;
}
};
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有