通用图像装载机的正确使用 [英] Correct usage of Universal Image Loader

查看:118
本文介绍了通用图像装载机的正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在努力优化我的照片库现在天(这是我的第一个Android项目)。所有的照片都是通过Web页面加载。我已经开始使用通用图像装载机,但我仍然不满足的结果。

Ok, I've been trying to optimize my photo gallery for days now (this is my first Android project). All photos are loaded via Web page. I've started using Universal Image Loader, but I'm still not content with results.

下面是我的类:

public class Galerija extends Activity {

ArrayList<RSSItem> lista = new ArrayList<RSSItem>();
ArrayList<String> lst_slika = new ArrayList<String>();
RSSItem tempItem = new RSSItem();
ImageAdapter adapter;
ImageLoader imageLoader;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_galerija);

    try 
    {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader myReader = sp.getXMLReader();

        URL url = new URL("http://erdut.gausstesting.info/generateXML/galerija.php");

        XMLHandler myXMLHandler = new XMLHandler();
        myReader.setContentHandler(myXMLHandler);
        myReader.parse(new InputSource(url.openStream()));
        lista = myXMLHandler.getRss_lista();
        lst_slika = lista.get(0).getImages();

    } catch (Exception e) {
        System.out.println(e);
    }

    adapter = new ImageAdapter(this, lst_slika);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(adapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });

} 

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> lista;   
    /*
    final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.mContext)
    .enableLogging()
    .memoryCacheSize(41943040)
    .discCacheSize(104857600)
    .threadPoolSize(10)
    .build();
    */
    final DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.ic_stub)
    .showImageForEmptyUri(R.drawable.ic_empty)
    .showImageOnFail(R.drawable.ic_error)
    .cacheInMemory()
    .cacheOnDisc()
    .build();

    public ImageAdapter(Context c, ArrayList<String> lista) {
        this.mContext = c;
        this.lista = lista;
        imageLoader = ImageLoader.getInstance();
        //imageLoader.init(config);
        imageLoader.init(ImageLoaderConfiguration.createDefault(c));
    }

    public int getCount() {
        return lista.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            //ova 3 polja označavaju veličinu, cropanje i padding prikazanih slika
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(lista.get(position), imageView, options);
        //new ImageDownloadTask(imageView).execute(lista.get(position));

        return imageView;
    }
}

}

这肯定已经加快东西比我的previous的解决方案,但我并不满足。首先,我甚至不能启动它在我的HTC Incredible S的 - 刚刚开始黑屏并没有什么负载的活动。这是为什么呢?

It has definitely sped things up compared to my previous solution, but I'm not content. First of all, I can't even start it on my HTC Incredible S - Just starts an activity with black screen and nothing loads. Why is that?

它仅适用于模拟器,但它重新加载所有图像为您滚动。所以,一旦你滚动下来,然后备份,所有的图像出现重新加载。那是它是如何想工作吗?任何进一步的方法,我可以改善呢?

It only works on emulator, but it reloads all the images as you scroll. So, once you've scrolled down and then back up, all images appear to be reloaded. Is that how it's suppose to work? Any further ways I can improve this?

在此先感谢!

推荐答案

您应该考虑使用的AsyncTask来从服务器获取网址。

You should consider using asynctask to get urls from the server.

您使用应该使用视图持有人平滑滚动和性能。 http://www.youtube.com/watch?v=wDBM6wVEO70 。该演讲是关于观点持有者和ListView性能。

You use should be using a view holder for smooth scrolling and performance. http://www.youtube.com/watch?v=wDBM6wVEO70. The talk is about view holder and listview performance.

http://developer.android.com/training/improving-布局/平滑scrolling.html

https://github.com/nostra13/Android-Universal-Image-Loader 。检查在配置和显示选项​​的话题。

而不是下载它再次,你应该在手机内存或SD卡缓存图像。

Instead of downloading it again you should cache images in phone memory or sdcard.

它使用URL作为键缓存中说SD卡图像(如果你已经正确配置)。如果从缓存中其他的下载,高速缓存和显示图像present显示。

It caches images in say sdcard (if you have configured properly) using url as the key. If present display from cache else download, cache and display images.

在自定义适配器的构造函数

In your custom adapter constructor

 File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "your folder");//for caching

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
 // You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.default_pic)//display stub image until image is loaded
.displayer(new RoundedBitmapDisplayer(20))
.build();

在你的getView()

In your getView()

viewholder.image=(ImageView)vi.findViewById(R.id.imageview); 
imageLoader.displayImage(imageurl, viewholder.image,options);//provide imageurl, imageview and options.

这篇关于通用图像装载机的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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