设为首页 加入收藏

TOP

自己定制ListView,上拉刷新和下拉刷新,加载网络图片,并且添加缓存机制。(三)
2017-10-11 17:09:32 】 浏览:10075
Tags:自己 定制 ListView 新和 下拉 刷新 加载 网络 图片 并且 添加 机制
e
226 Loadnetimage mLoadnetimage = new Loadnetimage(); 227 // 获取网络图片 228 Bitmap bitmap; 229 try { 230 bitmap = mLoadnetimage.loadRawDataFromURL(url); 231 return bitmap; 232 } catch (Exception e) { 233 e.printStackTrace(); 234 } 235 return null; 236 } 237 238 @Override 239 protected void onPostExecute(Object result) { 240 addbottom(result); 241 pd.dismiss(); 242 243 } 244 245 } 246 247 private void adddata(String url) { 248 // 设置缓存机制 249 Log.e("", "开始添加"); 250 251 for (int i = 0; i < cache.size(); i++) { 252 253 HashMap<String, Object> map = cache.get(i); 254 // 判断图片地址一样则载入缓存 255 if (url.equals(map.get(CACHE_KEY_TEXT))) { 256 Bitmap bitmap = (Bitmap) map.get(CACHE_KEY_IMAGE); 257 addtop(bitmap); 258 Log.e("", "载入缓存"); 259 260 return; 261 } 262 263 } 264 // 否则从网络新加载 265 Log.e("", "最新加载"); 266 new MyAsyncTaskTop(url).execute(); 267 268 } 269 270 private void addtop(Object result) { 271 272 // 新加载图片放入原始数据 273 HashMap<String, Object> map = new HashMap<String, Object>(); 274 map.put(KEY_IMAGE, result); 275 map.put(KEY_TEXT, data.size() + 1); 276 277 data.add(0, map); 278 279 mMyAdapter.notifyDataSetChanged(); 280 } 281 282 private void addbottom(Object result) { 283 HashMap<String, Object> map = new HashMap<String, Object>(); 284 map.put(KEY_IMAGE, result); 285 map.put(KEY_TEXT, data.size() + 1); 286 287 data.add(map); 288 289 mMyAdapter.notifyDataSetChanged(); 290 // 添加数据后自动滚动到底部 291 lv.setSelection(ListView.FOCUS_DOWN); 292 293 } 294 295 }
 1 package com.lixu.listviewrefresh;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.ByteArrayOutputStream;
 5 import java.io.InputStream;
 6 import java.net.HttpURLConnection;
 7 import java.net.URL;
 8 
 9 import android.graphics.Bitmap;
10 import android.graphics.BitmapFactory;
11 
12 public class Loadnetimage {
13     // 设置回调机制
14     
15     private OnLoadnetimageListener mOnLoadnetimageListener;
16 
17     public Loadnetimage() {
18 
19     }
20 
21     public interface OnLoadnetimageListener {
22         public void LoadnetimageListener(int count, int total);
23     }
24 
25     public void SetLoadnetimageListener(OnLoadnetimageListener l) {
26         mOnLoadnetimageListener = l;
27     }
28     
29     
30 
31     public Bitmap loadRawDataFromURL(String u) throws Exception {
32         
33         URL url = new URL(u);
34         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
35         // 设置请求的一些常用的参数
36         conn.setReadTimeout(30000);
37         conn.setConnectTimeout(30000);
38         conn.setDoInput(true);
39         conn.connect();
40         // 获取图片总数
41         int total = conn.getContentLength();
42 
43         InputStream is = conn.getInputStream();
44         BufferedInputStream bis = new BufferedInputStream(is);
45 
46         ByteArrayOutputStream baos = new ByteArrayOutputStream();
47         // 缓存2KB
48         final int BUFFER_SIZE = 2 * 1024;
49         final int EOF = -1;
50 
51         int count = 0;
52 
53         int c;
54         byte[] buf = new byte[BUFFER_SIZE];
55 
56         while (true) {
57             c = bis.read(buf);
58             if (c == EOF)
59                 break;
60             // 每次累加到现在为止我们下载了多少数据,以便于后面计算已经下载的数据占了总数量的百分比
61             count = count + c;
62             // 发布最新的数据,更新随后的进度条显示进度使用
63             if (mOnLoadnetimageListener != null)
64                 mOnLoadnetimageListener.LoadnetimageListener(count, total
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇自己定制ListView,上拉刷新和下.. 下一篇Material Design:CollapsingTool..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目