我从stackoverflow遵循了这个答案:
和 Github 上的一样:
https://github.com/thest1/LazyList
在的最后一行Basic Usage
,它说:
请仅创建一个 ImageLoader 实例并在您的应用程序中重用它。这种方式图像缓存将更加有效。
我怎样才能做到这一点?
我有很多 ListView(10-15),到目前为止,我在 Adapter 类中使用这种方式,如示例中所示:
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
我试过这样做:
在第一个类中创建一个静态变量,并在其他任何地方使用它:
Class first{
public static ImageLoader imageLoader;
}
Class LazyAdapter{
first.imageLoader = new ImageLoader(activity.getApplicationContext());
//use it
}
但这与创建不同的实例不一样吗?我的意思是我将在每个适配器中创建一个新实例,当然之前的实例已经消失了(没有更多参考)。
那么,这是正确的使用方法吗?
编辑:
这也写在代码中:
public File getFile(String url){
//I identify images by hashcode. Not a perfect solution, good for the demo.
String filename=String.valueOf(url.hashCode());
//Another possible solution (thanks to grantland)
//String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
return f;
}
那么,我应该使用String filename = URLEncoder.encode(url);
吗?
和
//使用可用堆大小的 25%
堆大小是指RAM吗?或可以分配给应用程序的最大 RAM(我在某处读到它是 19mb)。
因为根据设备(三星 Galaxy GT-S5830)规格:
内部 - 158 MB 存储,278 MB RAM
但在日志中我得到
01-23 06:23:45.679: I/MemoryCache(1712): MemoryCache will use up to 16.0MB
这是代码中设置的25%。
如果它是可用内存的 25%。然后在设置 -> 应用程序 -> 管理应用程序中:
在底部,它说159MB used 21MB free。
谢谢你