图片的缓存:
// 把bitmap存放到sd卡上
File file = new file("/sdcard/"+iconname);
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPDG,100,stream);//主要是调用这个函数compress(压缩)
// 把图片存放到内存缓存里面
Map<String, SoftReference<Bitmap>> iconCache;
iconCache.put(iconname,new SoftReference<Bitmap>(bitmap));
//从url中获得图片名
final String iconname = iconpath.substring(
iconpath.lastIndexOf("/") + 1, iconpath.length());
//是否加载软引用
if (iconCache.containsKey(iconname)) {
SoftReference<Bitmap> softref = iconCache.get(iconname);
if (softref != null) {
Bitmap bitmap = softref.get();
if (bitmap != null) {
System.out.println("使用内存缓存 ");
iv_book.setImageBitmap(bitmap);
} else {
loadimage(iv_book, book, iconname);
}
}
}
分页加载
数据量过大,比如1000条数据,你不可能一下子加载完,这时就需要分页加载,即,每次加载固定条目,不够,再继续加载内容
也就是说,从服务器上得到的数据传到list集合中,是分条加入list中的
何时加载数据 滚动超出页面
//滚动状态
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
}
//初始化的值
startindex = 1;
count = 5;
// 首先获取用户的 所有收集的信息
CollectionFeed feeds = myService.getUserCollections(uid,
"book", null, null, startindex, count);//从服务器得到的数据,每次是固定条目 startindex, count
List<Book> books = new ArrayList<Book>();//遍历这些条目,并加入list中
for (CollectionEntry ce : feeds.getEntries()) {
Subject se = ce.getSubjectEntry();
if (adapter == null) {
adapter = new MyReadAdapter(result);
subjectlist.setAdapter(adapter);
} else {
// 把新获取到的数据 加到listview的数据适配器里面
// 通知界面更新内容
adapter.addMoreBook(result);
// 通知数据适配器更新数据
adapter.notifyDataSetChanged();
}
//根据滚动状态进行判断
//主要是确定页面最后可见条目的数据
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE://空闲的
// 如果当前滚动状态为静止状态
// 获取listview中最后一个用户可见条目的位置
int positon = view.getLastVisiblePosition();//1
int totalcount = adapter.getCount();//2
if (positon == (totalcount - 1)) {//3 代表当前界面拖动到了最下方
// 获取更多的数据
startindex = startindex + count;//4
if (startindex > max) {//5
showToast("数据已经加载到最大条目");
return;
}
if (isloading) {
return;
}
fillData();
}
break;
}
}
如果系统内存还不足,可以用一个继承Application 重写onLowMemory() 方法,发送一个Intent
public class MyApp extends Application {
@Override
public void onLowMemory() {
super.onLowMemory();
// 发送一些广播 关闭掉一些activity service
Intent intent = new Intent();
intent.setAction("kill_activity_action");
sendBroadcast(intent);
}
}
private class KillReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
iconCache = null;
showToast("内存不足activity退出");
finish();
}
}
@Override
public void setupView() {
filter = new IntentFilter();
filter.addAction("kill_activity_action");
receiver = new KillReceiver();
this.registerReceiver(receiver, filter);
}
分页加载,添加上一页,下一页按钮
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_next:
startindex+=count;
if(startindex>100){
showToast("最多获取100条");
return ;
}
fillData();
break;
case R.id.bt_pre:
if(startindex>11){
startindex=startindex-count;
fillData();
}else{
showToast("已经翻到了第一页");
}
break;
}
}
图片的缓存 分页加载 系统内存不足
最新推荐文章于 2024-08-18 22:03:43 发布