java– 正确使用Universal Image Loader

java– 正确使用Universal Image Loader,第1张

概述好的,我一直在尝试优化我的照片库几天(这是我的第一个Android项目).所有照片都是通过网页加载的.我已经开始使用UniversalImageLoader,但我仍然不满意结果.这是我的班级:publicclassGalerijaextendsActivity{ArrayList<RSSItem>lista=newArrayList<RSSItem>();Arra

好的,我一直在尝试优化我的照片库几天(这是我的第一个Android项目).所有照片都是通过网页加载的.我已经开始使用Universal Image Loader,但我仍然不满意结果.

这是我的班级:

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;@OverrIDepublic 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;    }}

}

与我以前的解决方案相比,它确实加快了速度,但我并不满足.首先,我甚至无法在我的HTC Incredible S上启动它 – 只需启动黑屏活动,无需加载.这是为什么?

它仅适用于模拟器,但在滚动时会重新加载所有图像.因此,一旦您向下滚动然后再向上,所有图像似乎都会重新加载.它是如何工作的?还有什么方法可以改善这个吗?

提前致谢!

解决方法:

您应该考虑使用asynctask从服务器获取URL.

你应该使用视图持有人来平滑滚动和表现.
http://www.youtube.com/watch?v=wDBM6wVEO70.谈话是关于观看者和列表视图的表现.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

https://github.com/nostra13/Android-Universal-Image-Loader.检查“配置和显示选项”下的主题.

您应该将图像缓存在手机记忆库或SD卡中,而不是再次下载.

它使用url作为密钥,在sdcard中缓存图像(如果已正确配置).如果存在显示从缓存否则下载,缓存和显示图像.

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

 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()中

vIEwholder.image=(ImageVIEw)vi.findVIEwByID(R.ID.imagevIEw); imageLoader.displayImage(imageurl, vIEwholder.image,options);//provIDe imageurl, imagevIEw and options.
总结

以上是内存溢出为你收集整理的java – 正确使用Universal Image Loader全部内容,希望文章能够帮你解决java – 正确使用Universal Image Loader所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115531.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存