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; } }; };
以上就是使用Android 批量下載圖片的講解,如有疑問請留言或者到本站社區進行交流討論,大家共同進步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。