其他分享
首页 > 其他分享> > android – ListView加载图像时非常慢(使用Universal Image Loader)

android – ListView加载图像时非常慢(使用Universal Image Loader)

作者:互联网

我的listView只用文本运行非常流畅 – 但是一旦我尝试加载缩略图(甚至从缓存中),它就会运行SOO波动.

我正在使用Universal Image Loader script

我的ArticleEntryAdapter中的代码在公共View getView(…)方法中:

/**
     * 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显示它正在从缓存中加载图像(我认为):

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

我正在我的三星Galaxy Nexus上测试它并运行Android 4.0.4(虽然我的minSdkVersion =“8”)

解决方法:

将您的getView方法之外的ImageLoaderConfiguration配置,DisplayImageOptions imgDisplayOptions,ImageLoader imageLoader的瞬间作为Adapter类的私有字段/成员.你应该只创建一次这些东西,而不是每次调用getView.

编辑:没有看到你的整个Adapter类,这里是我正在说的东西.我希望它接近你拥有的东西,你可以使它成功. Lemme知道任何一种方式.

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
        }
    }

}

标签:android,listview,performance,image-loading
来源: https://codeday.me/bug/20190716/1480365.html