包含在Android的图片大的ListView [英] Large ListView containing images in Android

查看:144
本文介绍了包含在Android的图片大的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关各种Android应用程序,我需要大量的的ListView S,即这种看法100-300项。

For various Android applications, I need large ListViews, i.e. such views with 100-300 entries.

的所有条目必需散装加载应用程序时开始,如一些分类和加工是必要的,应用程序可以不知道第一显示,否则其中的物品。

All entries must be loaded in bulk when the application is started, as some sorting and processing is necessary and the application cannot know which items to display first, otherwise.

到目前为止,我已经加载的所有散装物品的图像,以及,然后将其保存在的ArrayList< CustomType> 一起的休息数据为每个条目

So far, I've been loading the images for all items in bulk as well, which are then saved in an ArrayList<CustomType> together with the rest of the data for each entry.

不过,当然,这不是一个很好的做法,因为你很可能有一个 OutOfMemoryException异常则:在<$ C $所有图像的参考文献C>的ArrayList prevent垃圾回收工作。

But of course, this is not a good practice, as you're very likely to have an OutOfMemoryException then: The references to all images in the ArrayList prevent the garbage collector from working.

所以最好的解决办法是,很明显,以而图像,然后加载必要的,正确的加载只有在大量的文本数据?在谷歌Play应用程序做到这一点,例如:你可以看到,当您滚动到它们的图像加载,即它们在适配器的 getView()方法很可能加载。但随着谷歌播放,这是一个不同的问题,反正图像必须从互联网上,这是不适合我的情况下被加载。我的问题不在于加载图像的时间过长,但它们存储需要太多的记忆。

So the best solution is, obviously, to load only the text data in bulk whereas the images are then loaded as needed, right? The Google Play application does this, for example: You can see that images are loaded as you scroll to them, i.e. they are probably loaded in the adapter's getView() method. But with Google Play, this is a different problem, anyway, as the images must be loaded from the Internet, which is not the case for me. My problem is not that loading the images takes too long, but storing them requires too much memory.

所以,我应该怎么办的图像?加载中 getView(),当真正需要它们?将使滚动缓慢。因此,调用一个的AsyncTask 呢?或者只是一个正常的?参数化呢?

So what should I do with the images? Load in getView(), when they are really needed? Would make scrolling sluggish. So calling an AsyncTask then? Or just a normal Thread? Parametrize it?

我可以拯救那些已经加载到的HashMap&LT的图像;字符串,位图&GT; ,这样他们就不需要重新加载 getView()。但是,如果这样做,你再有记忆问题:的HashMap 商店引用的所有图像,所以在最后,你可以有 OutOfMemoryException异常了。

I could save the images that are already loaded into a HashMap<String,Bitmap>, so that they don't need to be loaded again in getView(). But if this is done, you have the memory problem again: The HashMap stores references to all images, so in the end, you could have the OutOfMemoryException again.

我知道已经有很多的问题在这里的讨论图像的延迟加载。但是,他们主要包括加载速度慢的问题,没有过多的内存消耗。

I know that there are already lots of questions here that discuss "Lazy loading" of images. But they mainly cover the problem of slow loading, not too much memory consumption.

编辑:我现在已经决定的开始在getView AsyncTasks(),它加载图像到后台 ListView控件。但是,这将导致我的应用程序运行到RejectedExecutionException。我现在应该怎么办?

I've now decided to start AsyncTasks in getView() which load the image into the ListView in the background. But this causes my application to run into an RejectedExecutionException. What should I do now?

推荐答案

我把加载图像的AsyncTask的和附加的任务,在适配器的getView功能视图的方法来跟踪哪些任务加载中视图。我用这个在我的应用程序,而且也没有滚动的滞后,所有图像都加载在正确的位置上被抛出也不例外。另外,因为任务确实没有工作,如果是取消,您可以在列表上执行一扔,它应该落后了的。

I took the approach of loading the images with an AsyncTask and attaching the task to the view in the adapter's getView function to keep track of which task is loading in which view. I use this in an app of mine and there's no scroll lag and all images are loaded in the proper position with no exceptions being thrown. Also, because the task does no work if it's canceled, you can perform a fling on your list and it should lag up at all.

任务:

public class DecodeTask extends AsyncTask<String, Void, Bitmap> {

private static int MaxTextureSize = 2048; /* True for most devices. */

public ImageView v;

public DecodeTask(ImageView iv) {
    v = iv;
}

protected Bitmap doInBackground(String... params) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPurgeable = true;
    opt.inPreferQualityOverSpeed = false;
    opt.inSampleSize = 1;

    Bitmap bitmap = null;
    if(isCancelled()) {
        return bitmap;
    }

    opt.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(params[0], opt);
    while(opt.outHeight > MaxTextureSize || opt.outWidth > MaxTextureSize) {
        opt.inSampleSize++;
        BitmapFactory.decodeFile(params[0], opt);
    }
    opt.inJustDecodeBounds = false;

    bitmap = BitmapFactory.decodeFile(params[0], opt);
    return bitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
    if(v != null) {
        v.setImageBitmap(result);
    }
}

}

适配器存储包含所有需要加载的图像的文件路径的ArrayList。该getView功能如下:

The adapter stores an ArrayList that contains the file paths of all the images that need loaded. The getView function looks like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv = null;
    if(convertView == null) {
        convertView = getLayoutInflater().inflate(R.id.your_view, null); /* Inflate your view here */
        iv = convertView.findViewById(R.id.your_image_view);
    } else {
        iv = convertView.findViewById(R.id.your_image_view);
        DecodeTask task = (DecodeTask)iv.getTag(R.id.your_image_view);
        if(task != null) {
            task.cancel(true);
        }
    }
    iv.setImageBitmap(null);
    DecodeTask task = new DecodeTask(iv);
    task.execute(getItem(position) /* File path to image */);
    iv.setTag(R.id.your_image_view, task);

    return convertView;
}

请注意:只是一个警告这里,这可能仍给在版本1.5,你内存问题 - 因为它们使用的AsyncTask线程池2.3。 3.0+回到串行模型通过默认,用于执行AsyncTasks其保持它的一个任务同时运行,从而使用更少的内存在任何给定的时间。只要您的图片不是太大,虽然,你应该就可以了。

NOTE: Just a caveat here, this might still give you memory problems on versions 1.5 - 2.3 since they use a thread pool for AsyncTask. 3.0+ go back to the serial model by default for executing AsyncTasks which keeps it to one task running at a time, thus using less memory at any given time. So long as your images aren't too big though, you should be fine.

这篇关于包含在Android的图片大的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆