Android 實(shí)現(xiàn)單線程輪循機(jī)制批量下載圖片
創(chuàng)新互聯(lián)建站成立于2013年,先為玉屏等服務(wù)建站,玉屏等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為玉屏企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
listview 在為item 添加從網(wǎng)上下載下來的圖片時(shí), 如果每次都整合一個(gè)item時(shí)都需要new一個(gè)Thread去下載圖片,listview長時(shí)間滑動(dòng)時(shí)會(huì)產(chǎn)生大量線程。
用單線程輪循機(jī)制則可以解決這個(gè)問題
步驟如下:
1、需要一個(gè)任務(wù)集合
class imageViewTask{ String path; Bitmap bitmap; int position; }
// 任務(wù)集合 private List<imageViewTask> imageviews = new ArrayList<MusicAdapter.imageViewTask>();
2、在構(gòu)造方法中創(chuàng)建一個(gè)線程,通過任務(wù)集合中的path去網(wǎng)上下載圖片獲得bitmap并放置在這個(gè)任務(wù)中以Message的obj形式傳送給handler處理。
只有在為listview設(shè)置適配器時(shí)才需要?jiǎng)?chuàng)建這個(gè)工作線程, 且只有一個(gè)
(while(true)循環(huán) 在activity 調(diào)用OnDestroy )才會(huì)終止
// 獲得圖片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; // 發(fā)消息給主線程 handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }else{ synchronized (workThread) { try { //任務(wù)隊(duì)列為空則等待 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); // 將網(wǎng)上下載的圖片存入緩存集合中 map.put(ivt.path, new SoftReference<Bitmap>(bitmap)); ivt.bitmap = bitmap; return ivt; } }; workThread.start(); }
這個(gè)是httpUtils工具
public class HttpUtils { public final static int GET_METHOD = 1; public final static int POST_MEHTOD = 2; /** * 構(gòu)造方法 * @param uri 路徑 * @param method 發(fā)送消息模式 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方式發(fā)送消息 case GET_METHOD: HttpGet get=new HttpGet(uri); resp=client.execute(get); break; // 用post方式發(fā)送消息 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方法中在任務(wù)中放置圖片下載的path和position, 并為item中的imageview設(shè)置標(biāo)記, 為了在listview的item中放入圖片時(shí)的方便。
// 給imageview設(shè)置標(biāo)記 holder.iv.setTag(position); // 增加任務(wù)隊(duì)列 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; } }; };
以上就是使用Android 批量下載圖片的講解,如有疑問請(qǐng)留言或者到本站社區(qū)進(jìn)行交流討論,大家共同進(jìn)步,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
標(biāo)題名稱:Android實(shí)現(xiàn)單線程輪循機(jī)制批量下載圖片
文章網(wǎng)址:http://chinadenli.net/article46/pigeeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、域名注冊(cè)、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)