android加载更多的图片
这是昨天改进后的,我测试了下,可以加载图片到5万张,估计5万以上也是没问题的,我只试到5万,其实也没必要这么高,现实中1000左右就差不多了,不过我的应用到100就差不多了,
package com.lanlong.test;
import java.io.File;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class AndrodTestActivity extends Activity {
int number = 50000;
Drawable[] array;
Bitmap [] array2;
private Map<String, SoftReference<Drawable>> imageCache =
new HashMap<String, SoftReference<Drawable>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String iconPath = Environment.getExternalStorageDirectory()
+"/vpn/Bafangtong/image/weixin.png";
// array = new Drawable[number];//BitmapDrawable 一样
// array2 = new Bitmap[number];
File mfile = new File(iconPath);
Log.i("","mfile.exists() = "+mfile.exists());
Drawable draw = null;
long timeBegin = System.currentTimeMillis();
for(int i = 0; i < number; i++)
{
Log.e("", "测试第" + (i+1) + "张图片");
// time = 2second 10000张 8second 50000张 不到1秒, 29ms 100张
// array[i] = getResources().getDrawable(R.drawable.ic_launcher);//1000 ok
// array2[i] = BitmapFactory.decodeFile(iconPath);//max 222
// zoomImg(array2[i], 800, 480);
// array[i] = Drawable.createFromPath(iconPath);//max 221
// array[i] = BitmapDrawable.createFromPath(iconPath);//max 221
//time = 149second ,10000张 ; 1second ,100张 ; 901second ,50000张
addDrawableToCache(""+i, iconPath);
draw = getDrawableByPath(""+i, iconPath);
// array[i] = getBitmapByPath(""+i, iconPath);
Log.i("","draw = "+draw);
try {
// array[i] = Drawable.createFromStream(
// new FileInputStream(iconPath), iconPath);//max 111
// array[i] = BitmapDrawable.createFromStream(
// new FileInputStream(iconPath), iconPath);//max 111
// array2[i] = BitmapFactory.decodeStream(
// new FileInputStream(iconPath));//max 221 ;
// Log.i("", "array["+i+"] = "+array[i]);
} catch (Exception e) {
// TODO: handle exception
}
}
long timeEnd = System.currentTimeMillis();
Log.v("","time = "+(timeEnd - timeBegin)/1000 +" second");
}
public void addDrawableToCache(String id, String path) {
// 强引用的Bitmap对象
Drawable drawable = Drawable.createFromPath(path);
// 软引用的Bitmap对象
SoftReference<Drawable> softDrawable = new SoftReference<Drawable>(drawable);
// 添加该对象到Map中使其缓存
imageCache.put(id, softDrawable);
Log.w("","add, imageCache.size() = "+imageCache.size());
}
public Drawable getDrawableByPath(String id, String path) {
// 从缓存中取软引用的Bitmap对象
SoftReference<Drawable> softDrawable = imageCache.get(id);
// 判断是否存在软引用
if (softDrawable == null) {
return null;
}
// 取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空
Drawable drable = softDrawable.get();
return drable;
}
}
android加载更多的图片的更多相关文章
- Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】
Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)[系列1] Android在加载或者处理超大巨型图片 ...
- ajax点击加载更多数据图片(预加载)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- [Android] Android 支持下拉刷新、上拉加载更多 的 XRecyclerview
XRecyclerView一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView.使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊 ...
- Android 开发 上拉加载更多功能实现
实现思维 开始之前先废话几句,Android系统没有提供上拉加载的控件,只提供了下拉刷新的SwipeRefreshLayout控件.这个控件我们就不废话,无法实现上拉刷新的功能.现在我们说说上拉加载更 ...
- android 加载图片框架--Glide使用详解
一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...
- Android学习笔记_51_转android 加载大图片防止内存溢出
首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView, ...
- Android之Socket通信、List加载更多、Spinner下拉列表
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务 ...
- Android ListView加载更多
先看效果: ListView的footer布局: <?xml version="1.0" encoding="utf-8"?> <Relati ...
随机推荐
- jquery插件的编写
今天尝试了一下自己编写插件.最简单的jquery效果,返回顶部的按钮. 增加多个全局函数 添加多个全局函数,可采用如下定义: Java代码 jQuery.foo = function() { aler ...
- python image show()方法的预览问题
在windows下面使用PIL中Image的show()函数时,执行下列代码: from PIL import Image img = Image.open("1.png") ...
- VB生成xml
Dim text As XmlText Dim doc As New XmlDocument '加入XML的声明段落 Dim node As XmlNode = doc.CreateXmlDeclar ...
- 旧的VirtualBox News(从1.3.4开始)
https://linuxtoy.org/archives.html https://linuxtoy.org/archives/virtualbox-134.html http://www.cnbl ...
- iOS多线程系列(2)
前面了iOS的NSThread方法来实现多线程,这篇就简单的讲讲NSOperation和NSOperationQueue. NSOperation是一个抽象类,定义一个要执行的任务.NSOperati ...
- ArcGIS学习推荐基础教程摘录
###########-------------------摘录一--------------------------########### ***************************** ...
- PS快捷键大全
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C ...
- opennebula extend(expending) auth module ldap
LDAP Authentication addon permits users to have the same credentials as in LDAP, so effectively cent ...
- windows更改DNS设置
浏览器解析域名时,首先在本地的host文件中查找记录, HOSTS文件记录的地址在: 将注释去掉 访问ysp.tlmall.com 会访问127.0.0.1
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...