1

我从stackoverflow遵循了这个答案:

ListView 中图像的延迟加载

和 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

谢谢你

4

4 回答 4

3

作者提到在整个应用程序中只使用一个 ImageLoader 类的原因是您创建 ImageLoader 类实例的次数,然后在内部创建 MemoryCache 对象,这意味着 10 次 ImageLoader 实例然后 10 次 MemoryCache 对象。在 LazyList 项目中,1 个 MemoryCache 对象为图片预留了 App Heap 内存的 1/4。因此,由于许多 MemoryCache 实例,大多数应用程序堆内存已耗尽。在这里,我为您的问题提供了一个解决方案,即按照以下代码制作 MemoryCache 类 Singleton 类:

将 MemoryCache 中的构造函数更改为私有并进行以下修改。

private static MemoryCache mc;
    public static MemoryCache getMemoryCache()
    {
        if(mc==null)
        {
            mc = new MemoryCache();
        }
        return mc;
    }
    private MemoryCache(){
        //use 25% of available heap size
        setLimit(Runtime.getRuntime().maxMemory()/4);
    }
于 2013-02-20T07:11:49.247 回答
1

您应该使用单例模式,或者只是在某个类中初始化一个静态变量,然后在任何地方使用它。

public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader= someClass.imageLoader;
}
于 2013-02-20T07:12:41.137 回答
0

为什么不使用工厂方法来创建单例

在你的尝试这个代码片段ImageLoader.java

private synchronized static ImageLoader instance;

private ImageLoader(Context context) {
    this.context = context;
    fileCache = new FileCache(context);
    executorService = Executors.newFixedThreadPool(5);
}

public synchronized static ImageLoader getInstance(Context context) {
    if (instance == null)
        instance = new ImageLoader(context);
    return instance;
}
于 2013-02-20T07:14:39.483 回答
0

你可以这样做:

public class ImageLoader {

    private static ImageLoader inst;
    private ImageLoader() {

    }

    public static ImageLoader getInstance() {
        if (inst == null)
            inst = new ImageLoader(Context);
        return inst;
    }
}

每当您想要 ImageLoader 的对象时,您只需要调用getInstance()方法:ImageLoader.getInstance()

于 2013-02-20T07:15:37.010 回答