ListView控件时加载的图像非常慢(使用通用图像装载机)图像、控件、装载机、加载

2023-09-06 00:41:57 作者:无伴终老

我的ListView的运行速度非常流畅,只需文字 - 当我尝试加载在缩略图(甚至从缓存),但是,它运行SOO波涛汹涌

My listView runs very smooth with just text - but as soon as I try to load in thumbnails (even from cache), it runs SOO choppy.

我使用的是通用图像装载机脚本

中的code在我ArticleEntryAdapter 公开查看getView(...)方法:

The code in my ArticleEntryAdapter within public View getView(...) method:

/**
     * PHOTOS
     */
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.mContext)
        .enableLogging()
        .memoryCacheSize(41943040)
        .discCacheSize(104857600)
        .threadPoolSize(10)
        .build();

    DisplayImageOptions imgDisplayOptions = new DisplayImageOptions.Builder()
        //.showStubImage(R.drawable.stub_image)
        .cacheInMemory() 
        .cacheOnDisc() 
        //.imageScaleType(ImageScaleType.EXACT) 
        .build();

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    //loads image (or hides image area)
    imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
    if(article.photopath != null && article.photopath.length() != 0)
    {
        imageLoader.displayImage(
            "http://img.mysite.com/processes/resize_android.php?image=" + article.photopath + "&size=150&quality=80",
            viewHolder.thumbView,
            imgDisplayOptions
            );
        viewHolder.thumbView.setVisibility(View.VISIBLE);
    }
    else
    {
        viewHolder.thumbView.setVisibility(View.GONE); //hide image
        viewHolder.thumbView.invalidate(); //should call after changing to GONE
    }

LogCat中显示,它加载从缓存中(我认为)的图像:

Logcat shows that it's loading the images from cache (I think):

ImageLoader    Load image from memory cache [http://img.mysite.com/processes/...

我测试它在我的三星Galaxy Nexus和运行Android 4.0.4(虽然我的minSdkVersion =8)

I'm testing it on my Samsung Galaxy Nexus and running Android 4.0.4 (though my minSdkVersion="8")

推荐答案

把的instantation ImageLoaderConfiguration配置,DisplayImageOptions imgDisplayOptions,ImageLoader的ImageLoader的您getView方法,你的适配器类的私有字段/成员之外。您应该只有创造这些东西一次,而不是每次getView被调用。

Put the instantation of ImageLoaderConfiguration config, DisplayImageOptions imgDisplayOptions, ImageLoader imageLoader outside of your getView method as private fields/members of your Adapter class. You should only have create these things once, not everytime getView is called.

编辑:没有看到你的整个Adapter类,这里刺伤我在说什么。我希望它靠近你所拥有的,你可以把它工作了。还是让我知道,无论哪种方式。

Edit : without seeing your whole Adapter class, here a stab at what I'm saying. I hope its close to what you have and you can make it work out. Lemme know either way.

public class MyAdapterClass extends BaseAdapter {
    /**
     * PHOTOS
     */
    static ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.mContext)
        .enableLogging()
        .memoryCacheSize(41943040)
        .discCacheSize(104857600)
        .threadPoolSize(10)
        .build();

    static DisplayImageOptions imgDisplayOptions = new DisplayImageOptions.Builder()
        //.showStubImage(R.drawable.stub_image)
        .cacheInMemory() 
        .cacheOnDisc() 
        //.imageScaleType(ImageScaleType.EXACT) 
        .build();

    static ImageLoader imageLoader = ImageLoader.getInstance();
    /**
     * 
     */
    public MyAdapterClass() {
        // TODO Auto-generated constructor stub
        imageLoader.init(config);           
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getCount()
     */
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItem(int)
     */
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItemId(int)
     */
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        //loads image (or hides image area)
        imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
        if(article.photopath != null && article.photopath.length() != 0)
        {
            imageLoader.displayImage(
                "http://img.mysite.com/processes/resize_android.php?image=" + article.photopath + "&size=150&quality=80",
                viewHolder.thumbView,
                imgDisplayOptions
                );
            viewHolder.thumbView.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.thumbView.setVisibility(View.GONE); //hide image
            viewHolder.thumbView.invalidate(); //should call after changing to GONE
        }
    }

}