Android 实现单线程轮循机制批量下载图片
作者:小_练 时间:2022-11-05 11:03:45
Android 实现单线程轮循机制批量下载图片
listview 在为item 添加从网上下载下来的图片时, 如果每次都整合一个item时都需要new一个Thread去下载图片,listview长时间滑动时会产生大量线程。
用单线程轮循机制则可以解决这个问题
步骤如下:
1、需要一个任务集合
class imageViewTask{
String path;
Bitmap bitmap;
int position;
}
// 任务集合
private List<imageViewTask> imageviews = new ArrayList<MusicAdapter.imageViewTask>();
2、在构造方法中创建一个线程,通过任务集合中的path去网上下载图片获得bitmap并放置在这个任务中以Message的obj形式传送给handler处理。
只有在为listview设置适配器时才需要创建这个工作线程, 且只有一个
(while(true)循环 在activity 调用OnDestroy )才会终止
// 获得图片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();
}
这个是httpUtils工具
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();
}
}
3、在自定义adapter 的setView方法中在任务中放置图片下载的path和position, 并为item中的imageview设置标记, 为了在listview的item中放入图片时的方便。
// 给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();
}
4、传送消息给主线程,让hanler去更新UI
// 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;
}
};
};
来源:http://blog.csdn.net/qq_21046135/article/details/50670252
标签:Android,单线程,下载图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Retrofit实现多图片/文件、图文上传功能
2021-11-01 00:23:47
在Android中动态添加Panel框架的实现代码
2022-01-17 23:17:07
java高级用法之注解和反射讲义
2023-10-08 14:40:16
![](https://img.aspxhome.com/file/2023/3/65773_0s.png)
解决Android从相册中获取图片出错图片却无法裁剪问题的方法
2023-09-14 23:41:15
![](https://img.aspxhome.com/file/2023/3/139143_0s.png)
一篇文章带你了解Java Stream流
2022-11-05 10:26:18
误将.idea文件提交至git后删除的操作方法
2022-09-03 09:32:57
解决persistence.xml配置文件修改存放路径的问题
2023-07-16 09:53:54
Java虚拟机内存结构及编码实战分享
2023-11-29 13:47:47
![](https://img.aspxhome.com/file/2023/8/59518_0s.png)
android虚拟键盘弹出遮挡登陆按钮问题的解决方法
2022-03-06 15:54:54
![](https://img.aspxhome.com/file/2023/8/126708_0s.gif)
Android简单自定义音乐波动特效图
2022-10-09 15:45:44
![](https://img.aspxhome.com/file/2023/1/111701_0s.gif)
基于SpringCloudGateway实现微服务网关的方式
2021-12-02 04:33:04
![](https://img.aspxhome.com/file/2023/5/132195_0s.png)
Android实现多点触控功能
2021-10-18 08:39:33
![](https://img.aspxhome.com/file/2023/3/95993_0s.jpg)
java判断用户输入的是否至少含有N位小数的实例
2021-06-01 02:56:42
基于java实现租车管理系统
2022-02-08 12:48:49
![](https://img.aspxhome.com/file/2023/8/73038_0s.jpg)
Java Thread 类和Runnable 接口详解
2023-11-10 20:16:00
![](https://img.aspxhome.com/file/2023/7/59017_0s.png)
RecyclerView实现纵向和横向滚动
2023-04-03 14:50:44
![](https://img.aspxhome.com/file/2023/1/138031_0s.jpg)
C# Linq延迟查询的执行实例代码
2023-04-24 05:34:59
C#在Windows窗体控件实现内容拖放(DragDrop)功能
2021-07-25 01:48:27
![](https://img.aspxhome.com/file/2023/4/98254_0s.jpg)
Java 实战项目锤炼之IT设备固定资产管理系统的实现流程
2023-02-06 01:31:39
![](https://img.aspxhome.com/file/2023/3/125253_0s.jpg)
Java 如何使用Velocity引擎生成代码
2023-10-19 23:15:34
![](https://img.aspxhome.com/file/2023/9/97799_0s.png)